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 ...
Imageorbyte[]at the reception, right? - Aleksey TimoshchenkoCamera2Apithen it cannot work if the manifest indicates hardwareAccelerated = "false" ... In general, I will now try to compress the bitmap ... It may happen - Aleksey Timoshchenko