Out of memory error even after compressing the image

孤人 提交于 2019-12-13 15:13:58

问题


Here is my code please check and resolve my problem . In this i m getting image through image path i.e. i'm taking that in selectimage1, selectimage2...goes on and after that i have taken the image in Bitmap and then decode and reduce its size den also getting out of memory error .

so please help me to get out of it.

thanks

    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);

回答1:


You just need to add this line to your manifest file....It will allocate the large memory for your application..

android:largeHeap="true"



回答2:


Try That:

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

and also finish(); previous unusable activities.

Or

AndroidManifest.xml

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



回答3:


Try scaling down the Bitmap instead of compressing it. The official Android website has a very detailed guide for this: http://developer.android.com/training/displaying-bitmaps/



来源:https://stackoverflow.com/questions/22837320/out-of-memory-error-even-after-compressing-the-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!