更改图像库中的图片 [英] Change image to a picture in Gallery

查看:159
本文介绍了更改图像库中的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,需要获得来自Android设备的内置图片库随机图片,然后在屏幕上显示的应用程序。下面是我有工作:

- 一个ImageView的对象调用图片
-The ID,标题,数据,MIME_TYPE,我想要显示的图像尺寸

我觉得这个问题是我不知道我需要把在这一行什么样的信息:

  picture.setImageResource(???);

下面是我所有的code给你什么,我试图做一些了解:

 公共无效generateImage(){
    //获取由光标访问图像列表
    ContentResolver的CR = getActivity()getContentResolver()。
    的String [] =列新的String [] {
                    ImageColumns._ID,
                    ImageColumns.TITLE,
                    ImageColumns.DATA,
                    ImageColumns.MIME_TYPE,
                    ImageColumns.SIZE};
    光标光标= cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    列,NULL,NULL,NULL);    //收集图片的ID
    cursor.moveToFirst();
    ArrayList的<整数GT; picList =新的ArrayList<整数GT;();
    而(!cursor.isAfterLast()){
        picList.add(cursor.getInt(0));
        cursor.moveToNext();
    } //结束了    //生成随机数
    INT imageCount = picList.size() - 1;
    Log.d(图像计数=+ imageCount图像数);
    随机随机=新的随机();
    INT randomInt = random.nextInt(imageCount);
    //提取图像
    INT picID = picList.get(randomInt);
    picture.setImageResource(picID);} //最终生成图像

任何人有任何想法我需要什么,以设置图片对象,我从画廊有(preferably利用我已经获得的信息)?

图片做
解决方案

  //提取图像
参数:columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(randomInt);
文件路径= cursor.getString(参数:columnIndex);
imageView.setImageBitmap(德codeSampledBitmapFromResource(文件路径,宽,高));

和高效使用位图,这样你就不会遇到内存不足例外这里有两个函数从机器人开发者页面[链接的 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html ]

 公共位图德codeSampledBitmapFromResource(字符串路径,INT reqWidth,诠释reqHeight){        //首先去code。与inJustDe codeBounds = true来检查尺寸
        最后BitmapFactory.Options选项=新BitmapFactory.Options();
        options.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeFILE(路径选择);        //计算inSampleSize
        options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight);        //德code位与inSampleSize集
        options.inJustDe codeBounds = FALSE;
        返回BitmapFactory.de codeFILE(路径选择);
    }    公共静态INT calculateInSampleSize(BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
    //原始高度和图像宽度
    最终诠释身高= options.outHeight;
    最终诠释宽度= options.outWidth;    INT inSampleSize = 1;    如果(高度> reqHeight ||宽度GT; reqWidth){        //计算的高度和宽度的比率要求的高度和宽度
        最终诠释heightRatio = Math.round((浮点)高度/(浮点)reqHeight);
        最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth);        //选择最小比率inSampleSize值,这将保证
        //使用两个维度大于或等于所述最终图像
        //请求的高度和宽度。
        inSampleSize = heightRatio< widthRatio? heightRatio:widthRatio;
    }    返回inSampleSize;
}

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)?

解决方案

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

这篇关于更改图像库中的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆