如何从图库中获取图像并在Android应用程序中移动

时间:2013-08-01 12:23:25

标签: android bitmap gallery

我正在使用Android安卓应用,我必须锁定图库中的图像,所以我试图将图片从图库移动到我的应用程序中。

我正在使用此代码:

Cursor cursor = cursor1[0];
        int i = 0;
        while (cursor.moveToNext()) {

            String id = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID));
            long idC = Long.parseLong(id);
            Uri newUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, idC);
        try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(cr,newUri);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100,stream );

                Bitmap compBit = Bitmap.createScaledBitmap(bitmap, 100,100, true);
                bitmap1[i] = compBit;
                bitmap.recycle();

            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

但是得到了这个例外:

  

4192360字节分配时内存不足。

1 个答案:

答案 0 :(得分:3)

有一个关于有效显示位图的培训模块 - [link] http://developer.android.com/training/displaying-bitmaps/index.html

它提供了有关使用样本处理内存不足异常的良好信息。