Android 显示图像位图错误

发布于 2024-11-18 06:02:24 字数 5049 浏览 3 评论 0原文

您好,我正在尝试从 SD 卡中的所有文件夹中读取图像。因此,它会创建文件夹列表,例如下载、相机等。现在,当我按任何文件夹时,它会在网格视图中打开照片。我的下载文件夹包含小尺寸图像,应用程序工作正常,但是当我打开包含较大尺寸图像的相机文件夹时,我的应用程序崩溃了。我的网格视图获取功能如下。

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);  
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.localimageview, null);
        }

        BitmapFactory.Options options=new Options();
        options.inSampleSize=2;
        options.inTempStorage = new byte[16*1024];

        Bitmap bb=BitmapFactory.decodeFile(FileList.get(position).toString(),options);
        i.setImageBitmap(bb);

        i.setLayoutParams(new GridView.LayoutParams(75,75));  
        i.setScaleType(ImageView.ScaleType.CENTER_CROP);
        i.setPadding(8, 8, 8, 8);
        v = (View)i;
        return v;  
    }
}  

崩溃于
位图 bb=BitmapFactory.decodeFile(FileList.get(position).toString(),options);

    07-04 14:21:17.211: ERROR/AndroidRuntime(11509): FATAL EXCEPTION: main
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.ibm.imagegallery.ImageActivity$ImageAdapter.getView(ImageActivity.java:107)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.makeAndAddView(GridView.java:1219)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.makeRow(GridView.java:265)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.fillDown(GridView.java:218)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.fillFromTop(GridView.java:337)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.layoutChildren(GridView.java:1111)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.os.Looper.loop(Looper.java:123)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at dalvik.system.NativeStart.main(Native Method)
    07-04 14:21:17.221: WARN/ActivityManager(1378):   Force finishing activity com.ibm.imagegallery/.ImageActivity
    07-04 14:21:17.231: WARN/ActivityManager(1378):   Force finishing activity com.ibm.imagegallery/.ImageGallery

Hi I am trying to read images from all the folders in SD card. So its create folder list like download, camera etc. Now when I press any of the folder it open the Photos in the Grid view. My download folders has images of small size and the application works fine but when i open the camera folder which has bigger size images my application is crashing. My get view function of grid vies is as follows.

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);  
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.localimageview, null);
        }

        BitmapFactory.Options options=new Options();
        options.inSampleSize=2;
        options.inTempStorage = new byte[16*1024];

        Bitmap bb=BitmapFactory.decodeFile(FileList.get(position).toString(),options);
        i.setImageBitmap(bb);

        i.setLayoutParams(new GridView.LayoutParams(75,75));  
        i.setScaleType(ImageView.ScaleType.CENTER_CROP);
        i.setPadding(8, 8, 8, 8);
        v = (View)i;
        return v;  
    }
}  

}

Its crashing at
Bitmap bb=BitmapFactory.decodeFile(FileList.get(position).toString(),options);

    07-04 14:21:17.211: ERROR/AndroidRuntime(11509): FATAL EXCEPTION: main
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.ibm.imagegallery.ImageActivity$ImageAdapter.getView(ImageActivity.java:107)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.makeAndAddView(GridView.java:1219)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.makeRow(GridView.java:265)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.fillDown(GridView.java:218)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.fillFromTop(GridView.java:337)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.GridView.layoutChildren(GridView.java:1111)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.View.layout(View.java:7035)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.os.Looper.loop(Looper.java:123)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    07-04 14:21:17.211: ERROR/AndroidRuntime(11509):     at dalvik.system.NativeStart.main(Native Method)
    07-04 14:21:17.221: WARN/ActivityManager(1378):   Force finishing activity com.ibm.imagegallery/.ImageActivity
    07-04 14:21:17.231: WARN/ActivityManager(1378):   Force finishing activity com.ibm.imagegallery/.ImageGallery

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此刻的回忆 2024-11-25 06:02:24

检查例如这个问题。或者搜索一下,这是以前回答过的常见问题。

Check for example this question. Or do a search, this is a common problem answered before.

╭ゆ眷念 2024-11-25 06:02:24

当您访问相机图片时,一旦加载的图片太大,应用程序无法处理,您需要做的是降低图像质量并将其添加到您的活动中
这个问题的解决方案可以从这里找到
将图像加载到 Bitmap 对象时出现奇怪的内存不足问题

When You access the camera pictures the once which get loaded are too large to handle for the application what you need to do is that you need to degrade the images and get it on your activity
a solution to this problem can be taken from here
Strange out of memory issue while loading an image to a Bitmap object

愁杀 2024-11-25 06:02:24

尝试以下代码。希望有帮助

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewWrapper wrapper = null;

            try {
                if (row == null) {
                    LayoutInflater inflater = getLayoutInflater();
                    row = inflater.inflate(R.layout.gallery_template, parent,
                            false);
                    wrapper = new ViewWrapper(row);
                    row.setTag(wrapper);
                } else {
                    wrapper = (ViewWrapper) row.getTag();
                }

                Bitmap bitmap = getImage(position);

                if (bitmap != null ) {

                    wrapper.getIcon().setImageBitmap(bitmap);
                } else {
                                  showErrorDialogue(getString(R.string.noGalleryData));
                }
            } catch (Exception e) {}
            return (row);
        }

private Bitmap getImage(int position) {
        Bitmap bitmap = null;
        if (Constants.IMAGES_PATH.size() > 0) {

            try {
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inSampleSize = 4;


                String path = Constants.IMAGES_PATH.get(position);
                // -------- Decode THE BITMAP AND RESIZE IT
                bitmap = BitmapFactory
                    .decodeFile(path,options));
            } catch (Exception e) {}
        }
        return bitmap;
    }

Try following code.Hope this help

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewWrapper wrapper = null;

            try {
                if (row == null) {
                    LayoutInflater inflater = getLayoutInflater();
                    row = inflater.inflate(R.layout.gallery_template, parent,
                            false);
                    wrapper = new ViewWrapper(row);
                    row.setTag(wrapper);
                } else {
                    wrapper = (ViewWrapper) row.getTag();
                }

                Bitmap bitmap = getImage(position);

                if (bitmap != null ) {

                    wrapper.getIcon().setImageBitmap(bitmap);
                } else {
                                  showErrorDialogue(getString(R.string.noGalleryData));
                }
            } catch (Exception e) {}
            return (row);
        }

private Bitmap getImage(int position) {
        Bitmap bitmap = null;
        if (Constants.IMAGES_PATH.size() > 0) {

            try {
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inSampleSize = 4;


                String path = Constants.IMAGES_PATH.get(position);
                // -------- Decode THE BITMAP AND RESIZE IT
                bitmap = BitmapFactory
                    .decodeFile(path,options));
            } catch (Exception e) {}
        }
        return bitmap;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文