Skip to content Skip to sidebar Skip to footer

Change Image To A Picture In Gallery

I am working on an application that needs to obtain a random picture from the android device's built-in picture gallery and then display it on the screen. Here's what I have to wor

Solution 1:

Looks like you are saving an array of the cursor position... that would probably not give you much to work with. I think you'd rather populate an ArrayList with ImageColumns._ID and then use that string as a URI to open the image.

Solution 2:

//Extracting the image
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(randomInt);
filePath = cursor.getString(columnIndex);
imageView.setImageBitmap(decodeSampledBitmapFromResource(filePath,width,height));

And for efficiently using bitmaps so that you do not run into OutOfMemory Exceptions here are two functions startight from the androids developers page [link http://developer.android.com/training/displaying-bitmaps/load-bitmap.html]

public Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds = true to check dimensionsfinal BitmapFactory.Optionsoptions=newBitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path,options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

    publicstaticintcalculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of imagefinalintheight= options.outHeight;
    finalintwidth= options.outWidth;

    intinSampleSize=1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and widthfinalintheightRatio= Math.round((float) height / (float) reqHeight);
        finalintwidthRatio= Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee// a final image with both dimensions larger than or equal to the// requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

Post a Comment for "Change Image To A Picture In Gallery"