java.lang.OutOfMemoryError - BitmapFactory.de code(strPath中) [英] java.lang.OutOfMemoryError - BitmapFactory.decode(strPath)

查看:213
本文介绍了java.lang.OutOfMemoryError - BitmapFactory.de code(strPath中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的 java.lang.OutOfMemoryError ,每当我打电话 UploadActivity.java

行号176:

 位图BM = BitmapFactory.de codeFILE(strPath的);
 

查看我的日志:

  12-07 17:57:10.585:E / AndroidRuntime(16708):致命异常:主要
12-07 17:57:10.585:E / AndroidRuntime(16708):java.lang.OutOfMemoryError
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.graphics.BitmapFactory.nativeDe codeStream(本机方法)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:650)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.graphics.BitmapFactory.de codeFILE(BitmapFactory.java:389)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.graphics.BitmapFactory.de codeFILE(BitmapFactory.java:449)
12-07 17:57:10.585:E / AndroidRuntime(16708):在com.example.camera.UploadActivity $ ImageAdapter.getView(UploadActivity.java:176)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.AbsListView.obtainView(AbsListView.java:2465)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.ListView.makeAndAddView(ListView.java:1775)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.ListView.fillDown(ListView.java:678)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.ListView.fillFromTop(ListView.java:739)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.ListView.layoutChildren(ListView.java:1628)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.AbsListView.onLayout(AbsListView.java:2300)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.View.layout(View.java:14063)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.View.layout(View.java:14063)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.View.layout(View.java:14063)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.View.layout(View.java:14063)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.View.layout(View.java:14063)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1996)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1817)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1114)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4520)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.Choreographer.doFrame(Choreographer.java:525)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.os.Handler.handleCallback(Handler.java:615)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.os.Handler.dispatchMessage(Handler.java:92)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.os.Looper.loop(Looper.java:137)
12-07 17:57:10.585:E / AndroidRuntime(16708):在android.app.ActivityThread.main(ActivityThread.java:4921)
12-07 17:57:10.585:E / AndroidRuntime(16708):在java.lang.reflect.Method.invokeNative(本机方法)
12-07 17:57:10.585:E / AndroidRuntime(16708):在java.lang.reflect.Method.invoke(Method.java:511)
12-07 17:57:10.585:E / AndroidRuntime(16708):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1036)
12-07 17:57:10.585:E / AndroidRuntime(16708):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
12-07 17:57:10.585:E / AndroidRuntime(16708):在dalvik.system.NativeStart.main(本机方法)
 

UploadActivity.java: -

 公共类ImageAdapter扩展了BaseAdapter
        {
        私人上下文的背景下;

        公共ImageAdapter(上下文C)
        {
        // TODO自动生成方法存根
        上下文= C;
        }

        公众诠释getCount将(){
        // TODO自动生成方法存根
        返回ImageList.size();
        }

        公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回的位置;
        }

        众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回的位置;
        }

        公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
        // TODO自动生成方法存根

        LayoutInflater充气=(LayoutInflater)上下文
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        如果(convertView == NULL){
        convertView = inflater.inflate(R.layout.list_upload,NULL);
        }


        // ColImgName
        TextView的txtName的=(的TextView)convertView.findViewById(R.id.C​​olImgName);
        strPath的= ImageList.get(位置)的ToString();

        //获取文件名
        文件名= strPath.substring(strPath.lastIndexOf('/')+ 1,strPath.length());
        档案文件=新的文件(strPath的);
        @燮pressWarnings(未使用)
        长长度= file.length();
        txtName.setText(文件名);

        //图像资源
        ImageView的ImageView的=(ImageView的)convertView.findViewById(R.id.C​​olImgPath);
        位图BM = BitmapFactory.de codeFILE(strPath的);
        imageView.setImageBitmap(BM);


        // ColStatus
        最后ImageView的txtStatus =(ImageView的)convertView.findViewById(R.id.C​​olStatus);
        txtStatus.setImageResource(R.drawable.bullet_button);

        // 进度条
        最终进度进度=(进度)convertView.findViewById(R.id.progressBar);
        progress.setVisibility(View.GONE);

        // btnUpload
        最后的ImageButton btnUpload =(的ImageButton)convertView.findViewById(R.id.btnUpload);
        btnUpload.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
        //上传
        btnUpload.setEnabled(假);

        startUpload(位置);
        }
        });

        返回convertView;

        }
    }
 

解决方案

您需要回收的位图对象。

 位图BM = BitmapFactory.de codeFILE(strPath的);
    imageView.setImageBitmap(BM);
 

在的code在获取鉴于以上线路仅仅增加了code下面写的 ///现在回收您的位图,这将释放你的内存在每次迭代

 如果(BM!= NULL)
   {
     bm.recycle();
     BM = NULL;
    }
 

这也是,如果你得到同样的错误,在

替换低于code

 位图BM = BitmapFactory.de codeFILE(strPath的);
    imageView.setImageBitmap(BM);
 

 最后BitmapFactory.Options选项=新BitmapFactory.Options();
options.inSampleSize = 8;

位图BM = BitmapFactory.de codeFILE(strPath中,选项);
imageView.setImageBitmap(BM);
 

使用inSampleSize加载尺度位图到内存中。使用2的幂为inSampleSize值是更快,更有效的去codeR。但是,如果您打算缓存大小版本在内存或磁盘上,它通常还是值得进行解码,以最合适的图片尺寸以节省空间。

欲了解更多请参阅装入大位图有效地

I am getting java.lang.OutOfMemoryError, whenever i am calling UploadActivity.java

Line Number 176 is:

  Bitmap bm = BitmapFactory.decodeFile(strPath);

View my Log:

12-07 17:57:10.585: E/AndroidRuntime(16708): FATAL EXCEPTION: main
12-07 17:57:10.585: E/AndroidRuntime(16708): java.lang.OutOfMemoryError
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:449)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at com.example.camera.UploadActivity$ImageAdapter.getView(UploadActivity.java:176)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.AbsListView.obtainView(AbsListView.java:2465)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.ListView.makeAndAddView(ListView.java:1775)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.ListView.fillDown(ListView.java:678)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.ListView.fillFromTop(ListView.java:739)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.ListView.layoutChildren(ListView.java:1628)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.AbsListView.onLayout(AbsListView.java:2300)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.View.layout(View.java:14063)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.View.layout(View.java:14063)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.View.layout(View.java:14063)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.View.layout(View.java:14063)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.View.layout(View.java:14063)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewGroup.layout(ViewGroup.java:4607)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1996)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1817)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1114)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4520)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.Choreographer.doFrame(Choreographer.java:525)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.os.Handler.handleCallback(Handler.java:615)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.os.Looper.loop(Looper.java:137)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at android.app.ActivityThread.main(ActivityThread.java:4921)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at java.lang.reflect.Method.invokeNative(Native Method)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at java.lang.reflect.Method.invoke(Method.java:511)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
12-07 17:57:10.585: E/AndroidRuntime(16708):    at dalvik.system.NativeStart.main(Native Method)

UploadActivity.java:-

public class ImageAdapter extends BaseAdapter
        {
        private Context context;

        public ImageAdapter(Context c)
        {
        // TODO Auto-generated method stub
        context = c;
        }

        public int getCount() {
        // TODO Auto-generated method stub
        return ImageList.size();
        }

        public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
        }

        public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);      

        if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_upload, null);
        }


        // ColImgName
        TextView txtName = (TextView) convertView.findViewById(R.id.ColImgName);
        strPath = ImageList.get(position).toString();

        // Get File Name
        fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length() );
        File file = new File(strPath);
        @SuppressWarnings("unused")
        long length = file.length();
        txtName.setText(fileName);

        // Image Resource
        ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
        Bitmap bm = BitmapFactory.decodeFile(strPath);
        imageView.setImageBitmap(bm);


        // ColStatus
        final ImageView txtStatus = (ImageView) convertView.findViewById(R.id.ColStatus);
        txtStatus.setImageResource(R.drawable.bullet_button);

        // progressBar
        final ProgressBar progress = (ProgressBar) convertView.findViewById(R.id.progressBar);
        progress.setVisibility(View.GONE);

        //btnUpload
        final ImageButton btnUpload = (ImageButton) convertView.findViewById(R.id.btnUpload);
        btnUpload.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        // Upload
        btnUpload.setEnabled(false);

        startUpload(position);
        }
        });

        return convertView;

        }
    } 

解决方案

You need to recycle Bitmap object .

    Bitmap bm = BitmapFactory.decodeFile(strPath);
    imageView.setImageBitmap(bm);

After above lines of code in your get view just add the code written below ///now recycle your bitmap this will free up your memory on every iteration

    if(bm!=null)
   {
     bm.recycle();
     bm=null;
    }

After this also if you are getting same error the

Replace below code

    Bitmap bm = BitmapFactory.decodeFile(strPath);
    imageView.setImageBitmap(bm);

with

 final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;

Bitmap bm = BitmapFactory.decodeFile(strPath,options);
imageView.setImageBitmap(bm);

Use inSampleSize to load scales bitmaps to memory. Using powers of 2 for inSampleSize values is faster and more efficient for the decoder. However, if you plan to cache the resized versions in memory or on disk, it’s usually still worth decoding to the most appropriate image dimensions to save space.

For more see Loading Large Bitmaps Efficiently

这篇关于java.lang.OutOfMemoryError - BitmapFactory.de code(strPath中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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