我有画廊意图将imege复制到文件夹,以及ListView中显示的所有图像。我的压缩方法中有一个OutOfMemory异常和位图解码 - 修复它的最简单方法是什么?我有recycle()选项,但没有帮助。
这是我的onActivityResult并压缩:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
//Toast.makeText(this, selectedImagePath, Toast.LENGTH_SHORT).show();
File sdcard = Environment.getExternalStorageDirectory();
String path = "MANUAL/workflow/img" + actions.id() + ".png";
String pathCom = "/MANUAL/workflow/img" + actions.id() + ".png";
File from = new File(selectedImagePath);
File to = new File(sdcard, path);
try {
copy(from, to);
compress(pathCom);
} catch (IOException io){}
//from.renameTo(to);
}
}
}
public void compress(String comp) {
try {
String path = Environment.getExternalStorageDirectory() + comp;
BitmapFactory.Options buffer = new BitmapFactory.Options();
buffer.inSampleSize = 1;
Bitmap bmp = BitmapFactory.decodeFile(path, buffer);
if (bmp.getWidth() > width || bmp.getHeight() > height){
Bitmap resized = Bitmap.createScaledBitmap(bmp,(int)(bmp.getWidth()*0.2), (int)(bmp.getHeight()*0.2), true);
bmp.recycle();
FileOutputStream out = new FileOutputStream(path);
resized.compress(Bitmap.CompressFormat.PNG, 70, out);
resized.recycle();
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("saveBitmap", e.getMessage());
}
}
她的意图是:
switch (item.getItemId()) {
case R.id.gallery:
// in onCreate or any event where your want the user to
// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
答案 0 :(得分:0)
你的inSampleSize
设置为1,这意味着它基本上没有被使用.. 1 =没有下采样2将是1 / 2,1 / 1 1/4。
也请查看此http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
希望有所帮助。如果这样做的目的只是简单地创建一个缩放的图像文件,也许该任务应该在它自己的线程上使用AsyncTask来处理该图像的大小调整