【问题标题】:Out of memory error even after compressing the image压缩图像后内存不足错误
【发布时间】:2014-04-03 12:09:44
【问题描述】:

这是我的代码,请检查并解决我的问题。 在这个我通过图像路径获取图像,即我在selectimage1selectimage2...继续,之后我在位图中拍摄了图像,然后解码并减小它的大小den也内存不足错误。

所以请帮我摆脱它。

谢谢

    location=(TextView) findViewById(R.id.location);
    category=(TextView) findViewById(R.id.category);
    subcategory=(TextView) findViewById(R.id.subcategory);
    title=(EditText) findViewById(R.id.title);
    description=(EditText) findViewById(R.id.description);
    phonenumber=(EditText) findViewById(R.id.phonenumber);
    email=(EditText) findViewById(R.id.email);
    price=(EditText) findViewById(R.id.price);
    postadd=(Button) findViewById(R.id.button_post);


    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    locationname = preferences.getString("Location", "Location");

    SharedPreferences preferences1 = PreferenceManager.getDefaultSharedPreferences(this);
    categoryname = preferences1.getString("categoryname", "categoryname");

    SharedPreferences imagepath1 = PreferenceManager.getDefaultSharedPreferences(this);
    selectedImagePath1 = imagepath1.getString("picturePath1", "picturePath1");


    SharedPreferences imagepath2 = PreferenceManager.getDefaultSharedPreferences(this);
    selectedImagePath2 = imagepath2.getString("picturePath2", "picturePath2");

    SharedPreferences imagepath3 = PreferenceManager.getDefaultSharedPreferences(this);
    selectedImagePath3 = imagepath3.getString("picturePath3", "picturePath3");

    SharedPreferences imagepath4 = PreferenceManager.getDefaultSharedPreferences(this);
    selectedImagePath4 = imagepath4.getString("picturePath4", "picturePath4");


    Bitmap thumbnail1 = (BitmapFactory.decodeFile(selectedImagePath1));
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    thumbnail1.compress(Bitmap.CompressFormat.JPEG, 30, stream);

    Bitmap thumbnail2 = (BitmapFactory.decodeFile(selectedImagePath2));
    ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
    thumbnail2.compress(Bitmap.CompressFormat.JPEG, 30, stream1);

    Bitmap thumbnail3 = (BitmapFactory.decodeFile(selectedImagePath3));
    int size3 = 10;
    Bitmap bitmapsimplesize3 = Bitmap.createScaledBitmap(thumbnail3 ,thumbnail3.getWidth() / size3, thumbnail3.getHeight() / size3, true);

    Bitmap thumbnail4 = (BitmapFactory.decodeFile(selectedImagePath4));
    int size4 = 10;
    Bitmap bitmapsimplesize4 = Bitmap.createScaledBitmap(thumbnail4 ,thumbnail4.getWidth() / size4, thumbnail4.getHeight() / size4, true);

    imageview1.setImageBitmap(thumbnail1);
    imageview2.setImageBitmap(thumbnail2);
    imageview3.setImageBitmap(bitmapsimplesize3);
    imageview4.setImageBitmap(bitmapsimplesize4);

    location.setText(locationname);
    category.setText(categoryname);
    subcategory.setText(subcategoryname);
    subcategory.setVisibility(View.VISIBLE);

【问题讨论】:

    标签: android bitmap imageview


    【解决方案1】:

    您只需将这一行添加到您的清单文件中......它将为您的应用程序分配大内存......

    android:largeHeap="true"
    

    【讨论】:

      【解决方案2】:

      试试看:

      imageview1.setImageBitmap(null);
      imageview2.setImageBitmap(null);
      imageview3.setImageBitmap(null);
      imageview4.setImageBitmap(null);
      imageview1.setImageBitmap(thumbnail1);
      imageview2.setImageBitmap(thumbnail2);
      imageview3.setImageBitmap(bitmapsimplesize3);
      imageview4.setImageBitmap(bitmapsimplesize4);
      

      还有finish();以前无法使用的活动。

      或者

      AndroidManifest.xml

      <application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:largeHeap="true"
          android:theme="@style/AppTheme" >
      

      【讨论】:

      • 我可以使用其他人可以使用的共享首选项,因为在共享首选项中它包含最后一张图片
      【解决方案3】:

      尝试缩小位图而不是压缩它。 Android 官方网站对此有非常详细的指南:http://developer.android.com/training/displaying-bitmaps/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 1970-01-01
        • 1970-01-01
        • 2018-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多