我正在创建一个应用程序,我在其中创建一个相册,其中我从图库中选择图像,但是当我上传更大尺寸的图像时,尽管压缩图像,我仍然收到了outofmemoryexception。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
columnindex = cursor.getColumnIndex(filePathColumn[0]);
file_path = cursor.getString(columnindex);
// Log.e("Attachment Path:", attachmentFile);
// tv_attach.setText(file_path);
Log.e("file_path",file_path);
URI = Uri.parse("file://" + file_path);
//Retrieve filename and extension
Filename imageFileName = new Filename(file_path, '/', '.');
extension=imageFileName.extension();
fileName=imageFileName.filename();
System.out.println("Extension = " + imageFileName.extension());
System.out.println("Filename = " + imageFileName.filename());
//System.out.println("Path = " + imageFileName.path());
cursor.close();
uploadImageName=fileName+"."+extension;
imagename.setText(uploadImageName);
/*ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile( file_path));*/
Bitmap image=BitmapFactory.decodeFile(file_path);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
// image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
// String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
albumThumbnail = Base64.encodeBytes(byte_arr);
}
}
logcat的: -
06-03 18:41:08.680: E/AndroidRuntime(1942): FATAL EXCEPTION: main
06-03 18:41:08.680: E/AndroidRuntime(1942): java.lang.OutOfMemoryError
06-03 18:41:08.680: E/AndroidRuntime(1942): at java.lang.String.<init>(String.java:433)
06-03 18:41:08.680: E/AndroidRuntime(1942): at java.lang.String.<init>(String.java:276)
06-03 18:41:08.680: E/AndroidRuntime(1942): at com.example.freejeetoproject.Base64.encodeBytes(Base64.java:834)
06-03 18:41:08.680: E/AndroidRuntime(1942): at com.example.freejeetoproject.Base64.encodeBytes(Base64.java:719)
06-03 18:41:08.680: E/AndroidRuntime(1942): at com.example.freejeetoproject.CreateAlbum.onActivityResult(CreateAlbum.java:152)
06-03 18:41:08.680: E/AndroidRuntime(1942): at android.app.Activity.dispatchActivityResult(Activity.java:3988)
06-03 18:41:08.680: E/AndroidRuntime(1942): at android.app.ActivityThread.deliverResults(ActivityThread.java:2651)
06-03 18:41:08.680: E/AndroidRuntime(1942): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2697)
06-03 18:41:08.680: E/AndroidRuntime(1942): at android.app.ActivityThread.access$2000(ActivityThread.java:124)
06-03 18:41:08.680: E/AndroidRuntime(1942): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
06-03 18:41:08.680: E/AndroidRuntime(1942): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 18:41:08.680: E/AndroidRuntime(1942): at android.os.Looper.loop(Looper.java:130)
06-03 18:41:08.680: E/AndroidRuntime(1942): at android.app.ActivityThread.main(ActivityThread.java:3806)
06-03 18:41:08.680: E/AndroidRuntime(1942): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 18:41:08.680: E/AndroidRuntime(1942): at java.lang.reflect.Method.invoke(Method.java:507)
06-03 18:41:08.680: E/AndroidRuntime(1942): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-03 18:41:08.680: E/AndroidRuntime(1942): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-03 18:41:08.680: E/AndroidRuntime(1942): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试试这段代码,
android:largeHeap="true"
答案 1 :(得分:0)
Bitmap image=BitmapFactory.decodeFile(file_path);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte [] byte_arr = stream.toByteArray();
image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
//add this line
image.recycle();
image = null;
位图占用大量内存,你应该释放位图,以便有更多内存可用