I ran into such a problem when I needed to just get a photo from the device’s memory and set it in ImageView for viewing ... At first I tried this method

imageView.setImageURI(URI.parse("path")); 

but here is the first pitfall and I got this error

Up to a texture (5312x2988, max = 4096x4096)

Then I got the idea to get just a photo in Image convert it to a Bitmap , compress and set it as a Bitmap .

And I had to install 2 more lines in the manifest, I didn’t understand why

 android:hardwareAccelerated="false" android:largeHeap="true" 

But I didn't understand how to get a photo in Image knowing its path, so I decided to initialize File specify the path during initialization, then get the bytes from the file and convert them to Bitmap , compress and set it in ImageView .

And it seems like everything turned out, but here was the last stone, it showed me not a fatal error, but

I / Choreographer: Skipped 90 frames! The application may be doing too much work on its main thread.

that I overload main thread .

I still do not understand why ...

To avoid this, I decided to wrap this whole thing in AsyncTask ... And in the end I got a rather cumbersome design and everything was just to put a picture for display to the user.

And I forgot to mention that by default the picture is displayed 90 degrees rotated so that I had to implement the function of rotating the picture ...

I ask the advice of experienced users, did I do it right? Or can you really shorten the code?

 public void ShowImage(View view) { new AsyncTask<Void, Void, Bitmap>(){ @Override protected Bitmap doInBackground(Void... params) { File file = new File("/storage/emulated/0/Android/data/com.example.aleksey.deleteit/files/Image/PhotosDir/IMG.jpeg"); byte[] bytes = new byte[(int) file.length()]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); long i = bis.read(bytes); System.out.println("Done !!!!!!!!!!!!!!!!!!!!!!!!! " + i); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); bitmap = rotate(bitmap, 90); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); ImageView image = (ImageView) findViewById(R.id.imageView); if (image != null) { image.setImageBitmap(bitmap); } } }.execute(); } private Bitmap rotate(Bitmap bitmap, int degrees) { if (degrees != 0 && bitmap != null) { Matrix m = new Matrix(); m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2); Bitmap b2 = Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); if (bitmap != b2) { bitmap.recycle(); bitmap = b2; } } return bitmap; } } 

And by the way, you still feel the delay after clicking the "Show" button and until the show ...

  • one
    Why not use some kind of library for this task where everything is already done for you? For example, Picasso, Glide, and many others. But if you really want to download it yourself, here it’s written how best to do it - developer.android.com/intl/ru/training/displaying-bitmaps/… - temq
  • Oh thanks! Good advice, the libraries are really very convenient, I did not know ... 3 lines of code took up everything, only I tried and I see that none of the others work with Image or byte[] at the reception, right? - Aleksey Timoshchenko
  • And anyway, no matter how cool you even add libraries, android: hardwareAccelerated = "false" in the manifest and this is android: largeHeap = "true", otherwise it writes an error as I already described in question ... but here again the problem is as I work with Camera2Api then it cannot work if the manifest indicates hardwareAccelerated = "false" ... In general, I will now try to compress the bitmap ... It may happen - Aleksey Timoshchenko

1 answer 1

In general, in the end I solved the problem using the Glide library, it is the best one I looked at, its main advantage to me was that it takes several times less hip memory when compared to others.

Here is a link with a detailed description, it really helped me out, thanks for the hint @temq

There was no need to invent anything complicated and the code took 3 lines.

First you need to add

 dependencies { compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support:support-v4:22.0.0' } 

and then use it like this

 Glide.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(imageView); 

Very comfortable and no mistakes