Android:从特定目录加载图像

时间:2011-07-27 15:09:13

标签: android image gallery

我有一个在自定义目录中显示缩略图的图库。图库显示正常,但我无法通过单击缩略图打开完整图像。我的[无功能]点击监听器位于

之下
    try {
        if (LoadImageFiles() == true) {
            GridView imgGallery = (GridView) findViewById(R.id.gallery);

            final ImageAdapter ia = new ImageAdapter(PersonMedia.this);
            imgGallery.setAdapter(ia);

                            // Set up a click listener
            imgGallery.setOnItemClickListener(new OnItemClickListener() {

                  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                        String imgPath = paths.get(position);

                        Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                        intent.putExtra("filename", imgPath);
                        startActivity(intent);
                  }
            });
        }
    } catch (Exception ex) {
        Log.e("PersonMedia.LoadPictures", ex.getMessage());
    }

以下是图库的填充方式

//Declare a module level hashtable
private Hashtable<Integer, String> paths;



private boolean LoadImageFiles() {

try{
    mySDCardImages = new Vector<ImageView>();

    paths = new Hashtable<Integer, String>();

    fileCount = 0;

    sdDir = new File(imageDirectory);
    sdDir.mkdir();

    if (sdDir.listFiles() != null) 
    {
        File[] sdDirFiles = sdDir.listFiles();

        if (sdDirFiles.length > 0) 
        {               
            for (File singleFile : sdDirFiles) 
            {                   
                Bitmap bmap = decodeFile(singleFile);
                BitmapDrawable pic = new BitmapDrawable(bmap);

                ImageView myImageView = new ImageView(PersonMedia.this);                    
                myImageView.setImageDrawable(pic);
                myImageView.setId(mediaCount);      

                paths.put(fileCount, singleFile.getAbsolutePath());

                mySDCardImages.add(myImageView);

                mediaCount++;
                fileCount ++;
            }
         }
       }
     }
       catch(Exception ex){ Log.e("LoadImageFiles", ex.getMessage()); }

     return (fileCount > 0);
}

2 个答案:

答案 0 :(得分:1)

我能够通过使用哈希表来存储加载缩略图时图像的位置和路径来解决这个问题。以下是2个相关的代码片段

//Where the gallery is populated and the onclick is defined

private void PopulateGallery() {

    try {
        if (LoadImageFiles() == true) {
            GridView imgGallery = (GridView) findViewById(R.id.gallery);

            imgGallery.setAdapter(new ImageAdapter(PersonMedia.this));

            // Set up a click listener
            imgGallery.setOnItemClickListener(new OnItemClickListener() {

                  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                        String imgPath = paths.get(position);

                        Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                        intent.putExtra("filename", imgPath);
                        startActivity(intent);
                  }
            });
        }
    } catch (Exception ex) {
        Log.e("PersonMedia.LoadPictures", ex.getMessage());
    }

}


//Where the images are loaded. You'll need to create a module level hashtable

private Hashtable<Integer, String> paths;

private boolean LoadImageFiles() {

try{
    mySDCardImages = new Vector<ImageView>();

    paths = new Hashtable<Integer, String>();

    fileCount = 0;

    sdDir = new File(imageDirectory);
    sdDir.mkdir();

    if (sdDir.listFiles() != null) 
    {
        File[] sdDirFiles = sdDir.listFiles();

        if (sdDirFiles.length > 0) 
        {               
            for (File singleFile : sdDirFiles) 
            {                   
                Bitmap bmap = decodeFile(singleFile);
                BitmapDrawable pic = new BitmapDrawable(bmap);

                ImageView myImageView = new ImageView(PersonMedia.this);                    
                myImageView.setImageDrawable(pic);
                myImageView.setId(mediaCount);      

                paths.put(fileCount, singleFile.getAbsolutePath());

                mySDCardImages.add(myImageView);

                mediaCount++;
                fileCount ++;
            }
         }
       }
     }
       catch(Exception ex){ Log.e("LoadImageFiles", ex.getMessage()); }

     return (fileCount > 0);
}

答案 1 :(得分:0)

您的图片是否有扩展名,例如.jpg或.png?我不太确定,但看起来你正在寻找的是personId的字符串版本的图像,没有任何文件扩展名。

如果我错了,请纠正我。

另外请发布更多详细信息,例如您遇到的错误。

以下更新的答案。

当您向View.setTag(Object)添加项目时,请使用ListView(我猜)。您可以拨打以下内容。

view.setTag("imgFile.jpg");

然后从你的onClickListener内部,执行此操作:

String img = (String) getTag();
相关问题