Place a large image in ImageView without working

I think the problem is with my ImageView. I created a gallery where I can touch the image and put it in my ImageView below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/fonddegrade"> <Gallery android:layout_height="wrap_content" android:id="@+id/gallery" android:layout_width="fill_parent" /> <ImageView android:layout_below="@+id/gallery" android:layout_height="wrap_content" android:id="@+id/laphoto" android:layout_width="wrap_content" android:layout_centerHorizontal="true"/> 

This works great with a small image, but not with a large image (3264 * 1952). When I touch it (therefore, trying to put it in an ImageView), I have an error and my application crashes. Here is my java code to display the image:

  public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.photo); File images; // Trouver le bon endroit de stockage if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) images = new File("/sdcard/MyPerformance/Photo"); else images = this.getFilesDir(); images.mkdirs(); File[] imagelist = images.listFiles(new FilenameFilter(){ @Override public boolean accept(File dir, String name) { return ((name.endsWith(".jpg"))||(name.endsWith(".png"))); } }); mFiles = new String[imagelist.length]; for(int i = 0 ; i< imagelist.length; i++) { mFiles[i] = imagelist[i].getAbsolutePath(); } mUrls = new Uri[mFiles.length]; for(int i = 0; i < mFiles.length; i++) { mUrls[i] = Uri.parse(mFiles[i]); } imgView = (ImageView)findViewById(R.id.laphoto); if(mFiles.length != 0) imgView.setImageURI(mUrls[0]); gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { imgView.setImageURI(mUrls[position]); } }); } 

Either the problem arises from setImageURI (but I donโ€™t think this is the reason, since it works with a small image) or because of the size of the image.

What solution can you give me to solve this problem? You have received my thanks.

PS: Why is my "Hello" always deleted?

+6
source share
3 answers

Your image is probably too large for Android, and it goes out of memory. Applications can have only 16 MB of usable memory. Your image occupies 3264 * 1952 * 4 = ~ 25.5Mb (widthheightargb). Therefore, it is best to resize images to smaller sizes.

See: http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
Then: A strange memory issue when loading an image into a Bitmap object
Finally: VM ran out of memory while retrieving images from cache

+8
source

Here's a bitmap [use a class to help you deal with large bitmaps.

 import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class BitmapUtils { public static 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) { // Calculate ratios of height and width to requested height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } public static Bitmap decodeSampledBitmapFromResource(String pathToFile, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathToFile, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(pathToFile, options); } } 
+1
source

I searched the watch and went through all the links, and finally found it.

Glide.with (getContext ()) .load (selectedImageUri) .into (ImageView);

and glide does all the work with the backend. This made my day.

Glide Github Link: https://github.com/bumptech/glide

0
source

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


All Articles