Change image to a picture in Gallery

試著忘記壹切 提交于 2019-12-12 03:45:26

问题


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 work with:

-An ImageView object called picture -The ID, TITLE, DATA, MIME_TYPE, and SIZE of the picture I want to display

I think the problem is I don't know what information I need to put in this line:

picture.setImageResource(???);

Here's all my code to give you some idea of what I'm trying to do:

public void generateImage() {
    // Get list of images accessible by cursor
    ContentResolver cr = getActivity().getContentResolver();
    String[] columns = new String[] {
                    ImageColumns._ID,
                    ImageColumns.TITLE,
                    ImageColumns.DATA,
                    ImageColumns.MIME_TYPE,
                    ImageColumns.SIZE };
    Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    columns, null, null, null);

    // Collect Picture IDs
    cursor.moveToFirst();
    ArrayList<Integer> picList = new ArrayList<Integer>();
    while (!cursor.isAfterLast()) {
        picList.add(cursor.getInt(0));
        cursor.moveToNext();
    }// end for

    // Generate random number
    int imageCount = picList.size() - 1;
    Log.d("NUMBER OF IMAGES", "Image Count = " + imageCount);
    Random random = new Random();
    int randomInt = random.nextInt(imageCount);


    // Extract the image
    int picID = picList.get(randomInt);
    picture.setImageResource(picID);

}// end Generate Image

Anybody have any idea what I need to do in order to set the picture object to the picture that I have from the gallery (preferably using the information I've already obtained)?


回答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.




回答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 dimensions
        final BitmapFactory.Options options = new BitmapFactory.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);
    }

    public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;

    int inSampleSize = 1;

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

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = 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;
}


来源:https://stackoverflow.com/questions/13612376/change-image-to-a-picture-in-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!