SharedPreferences 不会发送位图:分配内存失败

Bitmap won't be sented by SharedPreferences: Failed to allocate memory

我在将编码位图发送到另一个时遇到了这个愚蠢的内存问题class:

java.lang.OutOfMemoryError: Failed to allocate a 5280012 byte allocation with 3645732 free bytes and 3MB until OOM

(之前可以,现在不知道是什么问题)

这是我的发件人 class:

    public void ProfilePic(View view) {

        Intent i = new Intent(Intent.ACTION_PICK , MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i , SELECTED_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode){
            case SELECTED_IMAGE:

                if (resultCode==RESULT_OK){

                    Uri uri=data.getData();
                    String[]projection = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getContentResolver().query(uri , projection , null , null , null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(projection[0]);
                    String filePath=cursor.getString(columnIndex);
                    cursor.close();

                   yourImage = BitmapFactory.decodeFile(filePath);

                    ProfilePic = new BitmapDrawable(yourImage);

                    imageView.setImageDrawable(ProfilePic);
                }

                break;
        }
    }


    public static String encodeTobase64(Bitmap yourImage) {

    Bitmap immage = yourImage;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);

    Log.d("Image Log:", imageEncoded);

    return imageEncoded;
}


    public void next2(View view) {

        if(yourImage != null) {

            SharedPreferences prefs = this.getSharedPreferences(
                    "PP", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = prefs.edit();
            edit.putString("PP", encodeTobase64(yourImage));
            edit.commit();

        }

        else {

            SharedPreferences prefs = this.getSharedPreferences(
                    "PP", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = prefs.edit();
            edit.putString("PP", "0");
            edit.commit();
        }


        SharedPreferences p = this.getSharedPreferences(
                "home_button", Context.MODE_PRIVATE);
        SharedPreferences.Editor e = p.edit();
        e.putBoolean("fr", true);
        e.commit();


        Intent i = new Intent(this , Results.class);
        startActivity(i);
    }

输入 "Results"-activity 时应用崩溃。

但是,关闭app再回来,直接进入Results-activity,一切正常(发送图片发送成功)...

是什么原因造成的?

BitmapFactory.Options 将帮助您简单地减小位图的大小。您可以手动决定大小,也可以简单地使用 inSampleSize 字段设置为所需的值。按照link到load large bitmap efficiently

  BitmapFactory.Options options=new BitmapFactory.Options();
  options.inSampleSize=4;
  yourImage =BitmapFactory.decodeFile(filePath,options)