Failed to save image file in android update. How to do it?

I cannot save the image file in Android Oreo (8.0) API 26.
The code works fine at API level 25 (7.0), and I did not find any changes in the documentation " Changes to the behavior of Android 8.0 "

Here is my code

String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); File myrootDir = new File(root); if (!myrootDir.exists()) { myrootDir.mkdir(); } File myDir = new File(root + "/Myimages"); if (!myDir.exists()) { myDir.mkdir(); } final String fname = System.currentTimeMillis()+"myimage.png"; File file = new File(myDir, fname); if (file.exists()) file.delete(); try { FileOutputStream out = new FileOutputStream(file); b.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); }catch (Exception e){ Log.e("MYAPP", "exception", e); } 

The exception is FileNotFoundException, there is no such file or directory. (And why not in android?)

 java.io.FileNotFoundException: /storage/emulated/0/Pictures/Myimages/1513151272243myimage.png (No such file or directory) 12-13 13:17:52.243 5839-5839/com.package.package W/System.err: at java.io.FileOutputStream.open0(Native Method) 12-13 13:17:52.243 5839-5839/com.package.package W/System.err: at java.io.FileOutputStream.open(FileOutputStream.java:287) 12-13 13:17:52.243 5839-5839/com.package.package W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:223) 12-13 13:17:52.243 5839-5839/com.package.package W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:171) 12-13 13:17:52.243 5839-5839/com.package.package W/System.err: at com.package.package.DetailPage$12.run(DetailPage.java:737) 12-13 13:17:52.244 5839-5839/com.package.package W/System.err: at android.os.Handler.handleCallback(Handler.java:789) 12-13 13:17:52.244 5839-5839/com.package.package W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98) 12-13 13:17:52.244 5839-5839/com.package.package W/System.err: at android.os.Looper.loop(Looper.java:164) 12-13 13:17:52.244 5839-5839/com.package.package W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541) 12-13 13:17:52.244 5839-5839/com.package.package W/System.err: at java.lang.reflect.Method.invoke(Native Method) 12-13 13:17:52.244 5839-5839/com.package.package W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 12-13 13:17:52.244 5839-5839/com.package.package W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
+10
source share
3 answers

There is, in fact, a small, minor change in the Permissions for applications running and oriented on API 26.

Previously, applications were automatically granted all permissions in this group if the user granted at least one permission in this group. This means that an application that was provided with READ_EXTERNAL_STORAGE would immediately be provided with WRITE_EXTERNAL_STORAGE , regardless of whether WRITE_EXTERNAL_STORAGE was explicitly requested.

As in Oreo, for applications oriented to API 26+, this has been fixed, and only those permissions that are explicitly requested will be granted. If the user has already granted permission in the same group, then there will be no invitation for a new permission, but he should still be requested.

That was the problem. When READ_EXTERNAL_STORAGE permission was granted to your application on Nougat or lower, you automatically received WRITE_EXTERNAL_STORAGE without requiring it specifically. When you try to perform the same procedure for saving a file in Oreo, you do not get WRITE_EXTERNAL_STORAGE automatically, so the record ultimately fails.

Just add a specific query for WRITE_EXTERNAL_STORAGE . If the user has already provided READ_EXTERNAL_STORAGE , they will not worry about another invitation. Alternatively, you can only query WRITE_EXTERNAL_STORAGE from the beginning, which implicitly includes READ_EXTERNAL_STORAGE , and saves you the need for two separate queries.

+15
source

I am also experiencing the same problem, and I am stuck on this issue from 2 months. I already gave permission to WRITE_EXTERNAL_STORAGE in the manifest first, and also checked again if it has write permission. I think this is not a resolution problem. Because when I test with oreo, a folder is created, and in the folder I create, there is a damaged image file. Because of this, he does not come to the gallery, and then saves it back. Image not uploaded. I get this problem only in android 7.1.1 and oreo. Upto android 7.0 works fine. I checked with samsung galaxy note 8 and google pixel xl 2 devices. I use a custom camera with gps function and below

 public void onImageAvailable(ImageReader reader) { Image image = null; try { image = reader.acquireLatestImage(); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] bytes = new byte[buffer.capacity()]; buffer.get(bytes); save(bytes); 

to save the image byte, and the save function contains the code below

 OutputStream output = null; try { output = new FileOutputStream(finalFile); output.write(bytes); } finally { if (null != output) { output.close(); } } 
+1
source

you decided? I have a similar problem, I'm trying to keep a backup of my database, which always worked, but now with Android Oreo the file is created, but empty.

0
source

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


All Articles