开发者

Android : Load image from specific directory

I have a gallery that is displaying thumbnail images in custom directories. The gallery displays fine, but I am unable to open the full image by clicking the thumbnail. My [non functioning] click listener is below

    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());
    }

Here is how the gallery is populated

//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());

                mySDCar开发者_JAVA百科dImages.add(myImageView);

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

     return (fileCount > 0);
}


I was able to resolve this by using a hashtable to store the position and path of the images when the thumbnails are loaded. Below are the 2 pertinent code snippets

//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);
}


Do your images have extensions, such as .jpg or .png? I'm not quite sure but it looks like you are looking for an image that is string version of your personId, without any file extension.

Please correct me if I'm wrong.

Also please post more details, such as the errors you are getting.

Updated answer below.

Use View.setTag(Object), when you are adding items to your ListView (I am guessing). You can call something like the following.

view.setTag("imgFile.jpg");

And then from inside of your onClickListener, just do this:

String img = (String) getTag();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜