Using android internal storage for images - android

I am trying to save the image file into my application's internal storage.
To do this, I wrote the following method which takes the id and bitmap and creates the file of an image, where directory is the class static field of type File.
// Returns thumbnail
private static Bitmap saveBitmapWithThumbnail(long id, Bitmap bitmap) {
Bitmap thumbnail;
if(directory == null) {
ContextWrapper cw = new ContextWrapper(Engine.getContext());
directory = cw.getDir(DIRECTORY_NAME, Context.MODE_PRIVATE);
if(!directory.exists()) directory.mkdir();
}
saveBitmap(getImageFileName(id), bitmap);
saveBitmap(getThumbnailFileName(id), thumbnail=makeThumbnail(bitmap));
return thumbnail;
}
What this method does is to get an id and bitmap and save it's original file and created thumbnail into the internal storage.
The names are generated as [id+".png"] for the original file and [id+"_tn.png"] for the thumbnail file.
saveBitmap() mehtod is the one which is writing the bitmap to the internal storage.
private static void saveBitmap(String fileName, Bitmap bitmap) {
File imageFile = new File(directory, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch(Exception e) {}
}
It takes the file name (11.png, 11_tn.png, ...) and the bitmap (whether it's original or thumbnail) and makes a file and compress into it's outputstream.
The weird thing is that when I run my application for the first time saving the images, it works great.
It can read and write the images that are stored int the internal storage.
However, after I close my application and run it again, it throws FileNotFoundException with following logs.
W/System.err: java.io.FileNotFoundException: /1.png: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:458)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:78)
W/System.err: at com.google.dotplace.dotplace.data.Image.parseCursor(Image.java:118)
W/System.err: at com.google.dotplace.dotplace.data.Image.checkDebug(Image.java:74)
W/System.err: at com.google.dotplace.dotplace.LoadActivity.debugDatabase(LoadActivity.java:77)
W/System.err: at com.google.dotplace.dotplace.LoadActivity.onCreate(LoadActivity.java:25)
W/System.err: at android.app.Activity.performCreate(Activity.java:5275)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2167)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2253)
W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:142)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:136)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5120)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
W/System.err: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
W/System.err: ... 19 more
Only conclusion I can make is that internal storage changes or it's files are delete after my application is closed, but I don't know why.
What I am guessing now is that I have problem with my testing.
I'm testing this as follows.
Compile and run the project with file insertions and check.
Compile and run the project without file insertions and check if 1's data is remaining.
I am doing this because I did't make any ui of writing or reading files in my app.
My only guess is when I compile the project again, it somehow changes something unique in the context of my application which leads to changed internal storage which I think it's quite nonsense.
Where am I wrong??

There are several things which you may be doing wrong. Whenever you want to read/write internal/external storage, you have to make sure you do the following things;
Put READ/WRITE Storage permissions in your Manifest.
If your Android Version is Marshmallow or greater, ask run-time permissions. You can get information about this here
Make sure the path is correct. Most of the time hard-coded paths make difference. Refer here for more detail.
Make sure you call mkdirs() if directory doesn't exist.
Make sure the filename is correct. You said earlier that it's 11.png and 11_tn.png but later you accessed 1.png. May be this is causing a problem.

you have to give runtime permission for marshmallow and above:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />`

Related

IO exception while creating file on android sdcard with permissions granted

I am trying to move file from internal phone memory to sd card.
val oldFile = File(oldPath) //oldPath is string
val newFile = File(newPath) //newPath is string
if(newFile.exists()) {
newFile.delete()
}
newFile.parentFile.mkdirs()
newFile.createNewFile() //crashes here
// copy file code
Paths are absolute.
Old file path is:
/storage/emulated/0/Android/data/com.package/files/2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c
New file path is:
/storage/48EE-C144/chosenDirectory/2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c
where 2ec37ce3-ca72-4a35-a6e6-2d7f8e864c6c is file.
Paths are proper and the new file path is chosen manually by user with external directory picker library which works fine.
Of course app asks user for permissions and does not shall pass user to app until permissions are not granted.
int perm = this.checkSelfPermission(permission.WRITE_EXTERNAL_STORAGE);
int perm2 = this.checkSelfPermission(permission.READ_EXTERNAL_STORAGE);
if (perm != PackageManager.PERMISSION_GRANTED || perm2 != PackageManager.PERMISSION_GRANTED) {
/*ask for permissions and show explain dialog*/
}
Parmissions in manifest are added too.
Application crashes while calling newFile.createNewFile() with exception log:
05-07 13:02:14.995 14004-14004/com.packagename E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.packagename, PID: 14004
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451) 
Caused by: java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
at java.io.File.createNewFile(File.java:948)
at com.packagename.backend.localApi.changePath.PathChanger.changePath(PathChanger.kt:29)
at com.packagename.backend.localApi.changePath.PathChangeService$changePath$1.onSelect(PathChangeService.kt:49)
at com.codekidlabs.storagechooser.fragments.ChooserDialogFragment$1.onItemClick(ChooserDialogFragment.java:169)
at android.widget.AdapterView.performItemClick(AdapterView.java:343)
at android.widget.AbsListView.performItemClick(AbsListView.java:1665)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:4075)
at android.widget.AbsListView$10.run(AbsListView.java:6552)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451) 
Need to add permission in AndroidManifest.xml file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Solution for me was stop using java classes and migrate to Storage Access Framework

mkdirs doesn't work on Android Oreo [duplicate]

I can't save an image file in android oreo(8.0) api 26.
The code is working perfectly in api level 25 (7.0) and I didn't find any changes in the documentation "Android 8.0 Behavior Changes"
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);
}
Exception is FileNotFoundException, No such file or directory. (But Why not in android n ?)
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)
There is, in fact, a slight, subtle change in Permissions for apps running on and targeting API 26.
Previously, apps were automatically granted all permissions in a given group if at least one permission in that group had been granted by the user. This means that an app that had been granted the READ_EXTERNAL_STORAGE would've had WRITE_EXTERNAL_STORAGE immediately granted to it as well, regardless of whether WRITE_EXTERNAL_STORAGE had been explicitly requested.
As of Oreo, for apps targeting API 26+, this has been corrected, and only those permissions that are explicitly requested will be granted. If the user has already granted a permission in the same group, then there will be no prompt for the new permission, but it still must be requested.
That was the problem, in this case. When the READ_EXTERNAL_STORAGE permission was granted to your app on Nougat or below, you were automatically getting WRITE_EXTERNAL_STORAGE, too, without having to request that one specifically. When you try the same file save procedure in Oreo, you aren't getting WRITE_EXTERNAL_STORAGE automatically, so the write ultimately fails.
Simply add a specific request for WRITE_EXTERNAL_STORAGE. If the user has already granted READ_EXTERNAL_STORAGE, they won't be bothered with another prompt. Alternatively, you could request solely WRITE_EXTERNAL_STORAGE from the start, which implicitly includes READ_EXTERNAL_STORAGE, and would save you the need for two separate requests.
I am also experiencing the same issue and i am stuck with this issue from 2 months. I have already given permission for WRITE_EXTERNAL_STORAGE in the manifest first and also again checking is it have write permission. I think it is not a permission issue. Because when i test with oreo the folder is creating and there is a corrupted image file in the folder i am creating. Because of that it is not coming to the gallery and then on save it returns me Image not captured. I am getting this problem only in android 7.1.1 and oreo. Upto android 7.0 it is working fine. I have checked with devices samsung galaxy note 8 and google pixel xl 2. I am using a custom camera with with gps and below function
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);
for image byte save 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();
}
}

Failed to create Directory in Oreo (API 26) [duplicate]

I can't save an image file in android oreo(8.0) api 26.
The code is working perfectly in api level 25 (7.0) and I didn't find any changes in the documentation "Android 8.0 Behavior Changes"
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);
}
Exception is FileNotFoundException, No such file or directory. (But Why not in android n ?)
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)
There is, in fact, a slight, subtle change in Permissions for apps running on and targeting API 26.
Previously, apps were automatically granted all permissions in a given group if at least one permission in that group had been granted by the user. This means that an app that had been granted the READ_EXTERNAL_STORAGE would've had WRITE_EXTERNAL_STORAGE immediately granted to it as well, regardless of whether WRITE_EXTERNAL_STORAGE had been explicitly requested.
As of Oreo, for apps targeting API 26+, this has been corrected, and only those permissions that are explicitly requested will be granted. If the user has already granted a permission in the same group, then there will be no prompt for the new permission, but it still must be requested.
That was the problem, in this case. When the READ_EXTERNAL_STORAGE permission was granted to your app on Nougat or below, you were automatically getting WRITE_EXTERNAL_STORAGE, too, without having to request that one specifically. When you try the same file save procedure in Oreo, you aren't getting WRITE_EXTERNAL_STORAGE automatically, so the write ultimately fails.
Simply add a specific request for WRITE_EXTERNAL_STORAGE. If the user has already granted READ_EXTERNAL_STORAGE, they won't be bothered with another prompt. Alternatively, you could request solely WRITE_EXTERNAL_STORAGE from the start, which implicitly includes READ_EXTERNAL_STORAGE, and would save you the need for two separate requests.
I am also experiencing the same issue and i am stuck with this issue from 2 months. I have already given permission for WRITE_EXTERNAL_STORAGE in the manifest first and also again checking is it have write permission. I think it is not a permission issue. Because when i test with oreo the folder is creating and there is a corrupted image file in the folder i am creating. Because of that it is not coming to the gallery and then on save it returns me Image not captured. I am getting this problem only in android 7.1.1 and oreo. Upto android 7.0 it is working fine. I have checked with devices samsung galaxy note 8 and google pixel xl 2. I am using a custom camera with with gps and below function
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);
for image byte save 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();
}
}

FileReader path error - File Not Found

I have included my root-CA.crt file in app/src/main/assets.
I have typed this and ran on the emulator.
PEMParser reader = new PEMParser(new FileReader("file:///android_asset/root-CA.crt"));
X509CertificateHolder caCertHolder = (X509CertificateHolder) reader.readObject();
reader.close();
And got this error:
10-18 20:47:34.601 10617-10617/? W/System.err: java.io.FileNotFoundException: /file:/android_asset/root-CA.crt: open failed: ENOENT (No such file or directory)
10-18 20:47:34.601 10617-10617/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:416)
10-18 20:47:34.601 10617-10617/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:78)
10-18 20:47:34.601 10617-10617/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:105)
10-18 20:47:34.611 10617-10617/? W/System.err: at java.io.FileReader.<init>(FileReader.java:66)
Why I am getting this error ?? I have already mentioned the correct path. But why?
file:///android_asset is only meaningful with WebView. It cannot be used elsewhere.
Use AssetManager and its open() method to get an InputStream on the content represented by your asset. Wrap that in an InputStreamReader as needed.

How do I fix an ANR handler throwing an exception?

I am debugging using an actual phone, and sometimes I am inspecting the contents of an Intent (which naturally arrives on the main thread) in the debugger. However, since I am blocking the main thread, the system throws an ANR. Afterwards, the app crashes and I see this in LogCat:
Here is the exception:
07-25 12:25:32.058 673-15203/system_process E/ActivityManager﹕ Error reading /data/anr/traces.txt
java.io.FileNotFoundException: /data/anr/traces.txt: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:409)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at android.os.FileUtils.readTextFile(FileUtils.java:159)
at com.android.server.am.ActivityManagerService$15.run(ActivityManagerService.java:8926)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:393)
            at java.io.FileInputStream.<init>(FileInputStream.java:78)
            at android.os.FileUtils.readTextFile(FileUtils.java:159)
            at com.android.server.am.ActivityManagerService$15.run(ActivityManagerService.java:8926)
Terminal shows that the file is actually not there. If I try and create it myself, it just gets deleted and the exception is still thrown. There is actually a different file: traces_com.foo.bar.txt. Maybe it is supposed to be looking for that, but isnt for some reason?

Categories

Resources