0

Many have asked this question before and got the right way to resolve it. but somehow its not working out for me. My app crashes when I try to choose a large image for display. I have tried to reduce the size by scaling it as well using BitmapFactory. This is not working. Please help.

my code:

    public void browse(View v){
            @SuppressWarnings("unused")
            int id = v.getId();
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, REQUEST_CODE);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            InputStream stream = null;

            if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {

                Uri imgUri = data.getData();
                logopath = getPath(imgUri);

                //This block is to show the view
                    try {
                        if(bitmap != null){
                            bitmap.recycle();
                        }
                            stream = getContentResolver().openInputStream(data.getData());
                            bitmap = BitmapFactory.decodeStream(stream);
                            iv.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 100, 100, true));
                            //bitmap = shrinkBitmap(logopath,100,100);
                            //iv.setImageBitmap(bitmap);
                        } 
                    catch (FileNotFoundException e) {
                            e.printStackTrace();
                    }
                finally{
                    if (stream != null) {
                        try {
                                stream.close();
                            } 
                        catch (Exception e) {
                                e.printStackTrace();
                        }
                    }
                }

            }
        }

        private Bitmap shrinkBitmap(String file, int height, int width) {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

            if (heightRatio > 1 || widthRatio > 1)
            {
             if (heightRatio > widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             } else {
              bmpFactoryOptions.inSampleSize = widthRatio; 
             }
            }

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
         return bitmap;
        }

LogCat details:

    06-25 07:16:26.056: E/dalvikvm-heap(1973): Out of memory on a 48000016-byte allocation.
    06-25 07:16:26.206: E/AndroidRuntime(1973): FATAL EXCEPTION: main
    06-25 07:16:26.206: E/AndroidRuntime(1973): java.lang.OutOfMemoryError
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at com.example.tg_db1.edit_company.onActivityResult(edit_company.java:113)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.os.Handler.dispatchMessage(Handler.java:99)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.os.Looper.loop(Looper.java:137)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at java.lang.reflect.Method.invoke(Method.java:511)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

使用抽样技术。采样值越大,图像尺寸越小。希望您能够处理 OutOfMemory 错误,该错误是由于低堆大小渲染大图像大小而导致的。

BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 15;



Bitmap bm = BitmapFactory.decodeStream(stream);

如果您使用的是 android 3.0 及以后版本,请在清单文件的应用程序标记中使用 android:largeHeap="true" 标记。

 <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:largeHeap="true">
于 2013-06-25T10:38:52.810 回答
0
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

参考

于 2013-06-25T07:58:00.473 回答