Will the image be too large for the application to return to the previous activity?

In AddMoreClaims activity has an imageView , button and saves a button .

When the button is pressed, it will go to activeGallery() and allow the user to select an image from the gallery. The selected image will be displayed on imageView AddMoreClaims .

  private void activeGallery() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, RESULT_LOAD_IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case RESULT_LOAD_IMAGE: if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver() .query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); photo = decodeSampledBitmapFromUri(picturePath,200,200); // make image clear imageView.setImageBitmap(photo); // display image on imageView } break; } public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) { Bitmap bm = null; // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 // and keeps both height and width larger than the requested // height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } } 

To make sure the image selected on imageView is clear, I added decodeSampledBitmapFromUri(picturePath,200,200) . So far, everything is working fine.

When you click the save button, the AddClaims listView image is returned .

  saveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent returnIntent = new Intent(); // back to AddClaims returnIntent.putExtra("photo", photo); setResult(Activity.RESULT_OK, returnIntent); finish(); } }); 

However, the above coding does not work. I'm not sure, because the image is too large, when I click the save button and want to return the image to the listView of AddClaims , it just goes back to activity before AddClaims. . But the code works for the selected image. Why is this going to happen?

+4
source share
4 answers

Not a good way to convey an image through the intention of returning. try writing it to an SD card, accessing it from another screen (then delete it if it is confidential)

A bitmap image usually has a size of 18 MB + ... So depending on the availability of the heap and all that it may or may not work. Even in high-performance devices, this can happen due to inaccessible heap space. Or you can clear unwanted items from the heap programmatically before doing this.

+2
source

First, it is advisable to decode your bitmaps asynchronously, rather than in the user interface stream. Secondly, do not transmit bitmaps with intent. There is also no need to write anything on the SD card, as the image is already on your device.

I recommend you use the image download library - you can see some of the best listed here .

The main goal of these libraries is to download, cache and display images from the Internet, but they are also great for displaying images from local storage.

For example, if you select Picasso , your code will look something like this:

 Picasso.with(this) .load(new File(picturePath)) .resize(yourTargetWidth, yourTargetHeight) .centerInside() .into(imageView); 

As you can see, the library generates, visits and displays a bitmap for you.

When you click the button, you can pass the picturePath to your other activity through Intet, where you can display the image using the same approach - Picasso.with(this)...

+2
source

You should follow this practice to avoid OOM exceptions:

To cache bitmap images, you can use any of the following libraries:

http://square.imtqy.com/picasso/

https://github.com/bumptech/glide

https://github.com/nostra13/Android-Universal-Image-Loader

0
source

To play with Bitmap, we need to optimize our code as much as possible. Either we have to give time for this, or use image loading libraries that are already highly optimized. I used Picaso in my project. It will take care of the image size and return the selected image bitmap. I used this code to get the image from the library and it works like a charm.

 Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, REQUEST_TAKE_GALLERY); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == REQUEST_TAKE_GALLERY) { Uri selectedImage = data.getData(); String[] filePath = {MediaStore.Images.Media.DATA}; Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null); c.moveToFirst(); int columnIndex = c.getColumnIndex(filePath[0]); String picturePath = c.getString(columnIndex); c.close(); Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); Log.e("path of image from gallery......******************.........", picturePath + ""); Picasso.with(mContext).load(new File(picturePath)).fit().centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE).into(mProfilePic); //Here mContext is context of Screen Context mContext; } } } 

When you need to send the selected image to another action. You can check this link.

fooobar.com/questions/50696 / ...

0
source

Source: https://habr.com/ru/post/1238405/


All Articles