选择大图像进行显示时,android图像视图崩溃 [英] android image view crashes when big image is chosen for display

查看:149
本文介绍了选择大图像进行显示时,android图像视图崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多人之前已经问过这个问题并找到了解决问题的正确方法。但不知怎的,它不适合我。当我尝试选择要显示的大图像时,我的应用程序崩溃了。我试图通过使用BitmapFactory缩放它来减小大小。这不起作用。请帮忙。

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.

我的代码:

    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详细信息:

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)


推荐答案

Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

推荐

这篇关于选择大图像进行显示时,android图像视图崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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