m0r6aN
m0r6aN

Reputation: 908

Android : Image thumbnail gallery from URI's stored in database

My application allows it's user to start a camera intent to take pictures to associate with people and evidence. I want to store the URI in a lookup table (ex: PersonMedia) and load thumbnails of the associated pictures into a gallery view; subsequently allowing the user to view the full image from the thumbnail. Every example I find demonstrates loading thumbnails from the SD Cards full contents but not from specific URI's.

My second choice being to store the byte array in SQLLite, but I'm not a big fan of that. Any help would be greatly appreciated.

As a side note, in the past I couldn't figure out how to vote on the correct answer. I finally got it! Yeah, I have my moments...lol.

Upvotes: 1

Views: 1099

Answers (1)

m0r6aN
m0r6aN

Reputation: 908

I solved this by loading images directly from the sd card by providing a dynamic directory path. Recalling the images by storing the URI's in the database didn't work the way I expected it to. Here is the code if anyone wants to do the same type thing.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.personmedia);

    personId = ((Global) this.getApplication()).getCurrentPersonID();       
    mediaCount = ((Global) getApplication()).getDataHelper().getPersonMediaCount(personId) + 1;
    imageDirectory = Environment.getExternalStorageDirectory() + "/CaseManager/" + Long.toString(personId);

    PopulateGallery();
}

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

}

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

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){    
    try {        
            //Decode image size        
            BitmapFactory.Options o = new BitmapFactory.Options();       
            o.inJustDecodeBounds = true;        
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);       

            //The new size we want to scale to        
            final int REQUIRED_SIZE=100;        

            //Find the correct scale value. It should be the power of 2.        
            int width_tmp=o.outWidth, height_tmp=o.outHeight;        
            int scale=1;        

            while(true){            
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)                
                    break;            
                width_tmp/=2;            
                height_tmp/=2;            
                scale*=2;        
            }        

            //Decode with inSampleSize        
            BitmapFactory.Options o2 = new BitmapFactory.Options();        
            o2.inSampleSize=scale;        

            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);    
        } 

        catch (FileNotFoundException e) {}    

        return null;
  }

Upvotes: 1

Related Questions