如何在共享首选项中保存缩放的位图?

SaDeGH_F

我试图将位图保存在sharedpreferences中,但是当我尝试加载该位图时,我给出了“ java.lang.OutOfMemoryError”。

我想我要保存未缩放的版本。

这是我的缩放代码:

public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);

}

这就是我保存位图的方式:

decodeFile(filePath);
img_logo.setImageBitmap(bmp);
settings = getSharedPreferences("pref", 0);
SharedPreferences.Editor prefsEditor = settings.edit();
prefsEditor.putString("photo1", filePath);
prefsEditor.commit();

怎么了?

编辑:

这是我的整个onActivityResult代码:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {


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

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

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


               decodeFile(filePath);
               img_logo.setImageBitmap(bmp);
               settings = getSharedPreferences("pref", 0);
               SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", filePath);
                    prefsEditor.commit();
                }


            } else {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
     else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

            decodeFile(capturedImageFilePath);

            if(data != null)
            {
                img_logo.setImageBitmap(bmp);
                settings = getSharedPreferences("pref", 0);
                SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", capturedImageFilePath);
                    prefsEditor.commit();
            }
        }
}
}

编辑2:

我的整个活动代码:

public class MainActivity extends Activity {
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
Uri mCapturedImageURI2;
SharedPreferences settings;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    settings = getSharedPreferences("pref", 0);

    img_logo = (ImageView) findViewById(R.id.imageView1);
    Log.d("SHAREDimagezzzz", "**********SHAREDzzzzzzz suckes******");
    img_logo.setImageURI(Uri.parse(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher)));
    Log.d("SHAREDimage", "**********SHARED suckes******");
    img_logo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startDialog();
        }
    });
}
protected void startDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("Upload Pictures Option");
    myAlertDialog.setMessage("How do you want to set your picture?");

    myAlertDialog.setPositiveButton("Gallery",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
                    galleryintent.setType("image/*");
                    galleryintent.putExtra("return-data", true);
                    startActivityForResult(galleryintent, GALLERY_PICTURE);
                }
            });

    myAlertDialog.setNegativeButton("Camera",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    ContentValues values = new ContentValues();  
                    values.put(MediaStore.Images.Media.TITLE, "fileName");  
                    mCapturedImageURI2 = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                    Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI2);  
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);

                }
            });
    myAlertDialog.show();
}
Bitmap bmp;
public String decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);
    return filePath;

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {

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

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

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

               String bb = decodeFile(filePath);
               img_logo.setImageBitmap(bmp);
               Log.d("galleryimage", "**********gallery suckes******");
               settings = getSharedPreferences("pref", 0);
               SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", bb);
                    prefsEditor.commit();
                }

            } else {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
     else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

            String bbd = decodeFile(capturedImageFilePath);

            if(data != null)
            {
                img_logo.setImageBitmap(bmp);
                Log.d("cameraimage", "**********camera suckes******");
                settings = getSharedPreferences("pref", 0);
                SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", bbd);
                    prefsEditor.commit();
            }
        }
}
}
}

我想我没有将sharedPreferences和prefsEditor放在正确的位置。

SaDeGH_F

我以某种方式解决了我的问题。

我知道这不是我问题的最佳答案,但它确实有效,对我来说就足够了!:)

在加载sharedPrefrences之前,Just应该再次进行扩展进度:

public class MainActivity extends Activity {
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
Uri mCapturedImageURI2;
SharedPreferences settings;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    settings = getSharedPreferences("pref", 0);
    Button b1 = (Button) findViewById(R.id.button1);
    Button b2 = (Button) findViewById(R.id.bto2);
    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startActivity(new Intent(MainActivity.this, A2.class));
        }
    });

    img_logo = (ImageView) findViewById(R.id.imageView1);
    Log.d("SHAREDimagezzzz", "**********SHAREDzzzzzzz suckes******");
    BitmapFactory.Options o3 = new BitmapFactory.Options();
    o3.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher), o3);
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o3.outWidth, height_tmp = o3.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }
    BitmapFactory.Options o4 = new BitmapFactory.Options();
    o4.inSampleSize = scale;
    Bitmap bit1 = BitmapFactory.decodeFile(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher), o4);

    img_logo.setImageBitmap(bit1);
    // img_logo.setImageURI(Uri.parse(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher)));

    Log.d("SHAREDimage", "**********SHARED suckes******");
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startDialog();
        }
    });

}
protected void startDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("Upload Pictures Option");
    myAlertDialog.setMessage("How do you want to set your picture?");

    myAlertDialog.setPositiveButton("Gallery",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
                    galleryintent.setType("image/*");
                    galleryintent.putExtra("return-data", true);
                    startActivityForResult(galleryintent, GALLERY_PICTURE);
                }
            });

    myAlertDialog.setNegativeButton("Camera",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    ContentValues values = new ContentValues();  
                    values.put(MediaStore.Images.Media.TITLE, "fileName");  
                    mCapturedImageURI2 = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                    Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI2);  
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);

                }
            });
    myAlertDialog.show();

}


Bitmap bmp;
public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);



}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {


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

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

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


               decodeFile(filePath);
               img_logo.setImageBitmap(bmp);
               Log.d("galleryimage", "**********gallery suckes******");
               settings = getSharedPreferences("pref", 0);
               Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", filePath);
                    prefsEditor.commit();
                }


            } else {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
     else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

            decodeFile(capturedImageFilePath);

            if(data != null)
            {
                img_logo.setImageBitmap(bmp);
                settings = getSharedPreferences("pref", 0);
                Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", capturedImageFilePath);
                    prefsEditor.commit();

            }

        }

}

}


}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在共享首选项中更改值

如何在Java中保存首选项用户设置?

如何在共享首选项中使用广播组?

如何在iOS的共享首选项中保存Cookie?

在共享首选项中保存大型数组列表

如何在Flutter上创建多个共享首选项?

如何在共享首选项中添加小部件

如何在flutter中通过共享的首选项保存所选颜色?

如何在Android Studio中保存具有共享首选项的listView [已解决]

如何在共享首选项中保存日历数据:Android

如何从共享首选项中保存和检索自定义Ararraist

如何在共享首选项中写入新值

如何在共享首选项中保存纬度和经度(位置)

如何使用ArrayList <String>保存/检索共享首选项

Gimp,如何在某些工具首选项中保存默认设置?

在共享首选项中保存R ID

如何在Android的“共享首选项”中使用主键?

如何使用“共享首选项”保存Position Android?

如何在首选项屏幕设置中保持活动

如何使用共享首选项保存数组列表项?

如何在共享首选项中保存以前的位置 (LatLang) 以及何时将新位置替换为以前的位置

如何在flutter中保存和加载对其他页面的共享首选项

如何在共享首选项中保存按钮状态?

搜索过滤列表视图后如何在共享首选项中保存元素的旧位置

如何在共享首选项中使用模型类保存数组列表?

如何在共享首选项中保存颜色

如何在 Android Studio 中使用共享首选项保存我的 listView

如何使用共享首选项保存评级?

我如何在共享首选项中保存数据并在颤振中将其接收到另一个页面