即使压缩了图像,仍然出现内存不足错误

3

以下是我的代码,请检查并解决我的问题。 我通过图片路径获取图像,即将其放入selectimage1selectimage2等,然后将图像取出到位图中,进行解码和大小缩减,但仍然遇到了内存不足的错误。

请帮助我解决这个问题。

谢谢。

    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);
3个回答

0

0

你只需要将这行代码添加到你的清单文件中...它会为你的应用程序分配大内存。

android:largeHeap="true"

0

试一下:

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" >

除了共享首选项,我还能使用什么?因为在共享首选项中它会保存最后一张图片。 - user3492351

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接