Will The Image Too Big Caused App Return To Previous Activity?
Solution 1:
Its not a good way to pass image via return intent. try writing it to sd card, access from other screen.(then delete it if its confidential)
An image bitmap usually have a size range of 18MB+...So depending upon Heap availability and all it may or may not work. Even in high end devices this may occur due to unavailability of space in Heap. Or you can clear unwanted items from heap in a programmatic way before doing this.
Solution 2:
First, it is preferable to decode your bitmaps asynchroniously, not on the UI thread. Second, do not pass bitmaps via intents. There is also no need to write anything on the sd card since the image is already on your device.
I recommend you to use an image loading library - you can see some of the best listed here.
Those libraries' main purpose is to download, cache and display images from the internet, but they also work great for displaying images from the local storage.
For example, if you choose Picasso, your code will be something like this:
Picasso.with(this)
.load(new File(picturePath))
.resize(yourTargetWidth, yourTargetHeight)
.centerInside()
.into(imageView);
As you can see, the library generates, crops and displays the bitmap for you.
When your button is clicked, you can pass picturePath
to your other Activity via Intet, where you can display the image using the same approach - Picasso.with(this)...
Solution 3:
You should follow these practice to avoid OOM exceptions:
- All the bitmap operations should be done on a worker thread. http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
- After processing, create cache of bitmap using bitmap file path.
- Pass this bitmap file path via intent to another activity.
- In new activity, get bitmap from cache using supplied file path.
For bitmap caching, you can use any of the below libraries:
http://square.github.io/picasso/
Solution 4:
For playing with Bitmap we have to optimize our code as much as we can. Either we have to give time for that or can use Image Loading Libraries which are much optimized already. I used Picaso in my project. It will take care of image size and will return selected image bitmap. I used this code for getting image from library and it work like charm.
Intentintent=newIntent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_TAKE_GALLERY);
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_GALLERY) {
UriselectedImage= data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursorc= getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
intcolumnIndex= c.getColumnIndex(filePath[0]);
StringpicturePath= c.getString(columnIndex);
c.close();
Bitmapthumbnail= (BitmapFactory.decodeFile(picturePath));
Log.e("path of image from gallery......******************.........", picturePath + "");
Picasso.with(mContext).load(newFile(picturePath)).fit().centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE).into(mProfilePic); //Here mContext is context of Screen Context mContext;
}
}
}
When you need to send selected image to other activity. You can check this link.
Post a Comment for "Will The Image Too Big Caused App Return To Previous Activity?"