IO exception while creating file on android sdcard with permissions granted - android

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

Related

Android Studio and SMBJ

I am trying to make a File List from my SMB Shared Folder with Android Studio.
I want to press a Button in my Android App and then get the List stored as *.txt in my root or SD Card Root.
I am using Android Studio API Version Level 30 and SMBJ Library.
But I'm failing in connection for my SMB Share.
When I press the button with the code its crashing without giving a Message on my Samsung Galaxy S20.
I found that the crash happens on following 2 lines:
AuthenticationContext ac = new AuthenticationContext("admin", "password123".toCharArray(),null);
Session session = connection.authenticate(ac);
I am not sure if I need the domain.
I have no domain installed and tried some different things.
SMBClient client = new SMBClient();
try {
Connection connection = client.connect("192.168.1.75");
AuthenticationContext ac = new AuthenticationContext("admin", "password123".toCharArray(),null);
Session session = connection.authenticate(ac);
//DiskShare share = (DiskShare) session.connectShare("Intel");
} catch (Exception e) {
txt.setText(e.toString());
}
The following is from the SMBJ Website on GIT:
https://github.com/hierynomus/smbj
SMBClient client = new SMBClient();
try (Connection connection = client.connect("SERVERNAME")) {
AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");
Session session = connection.authenticate(ac);
// Connect to Share
try (DiskShare share = (DiskShare) session.connectShare("SHARENAME")) {
for (FileIdBothDirectoryInformation f : share.list("FOLDER", "*.TXT")) {
System.out.println("File : " + f.getFileName());
}
}
}
This is the error log I get:
2022-04-25 11:45:27.475 22865-22865/com.example.filme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.filme, PID: 22865
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory;
at com.hierynomus.smbj.SMBClient.<clinit>(SMBClient.java:119)
at com.example.filme.MainActivity$1.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:7792)
at android.widget.TextView.performClick(TextView.java:16112)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7769)
at android.view.View.access$3800(View.java:910)
at android.view.View$PerformClick.run(View.java:30213)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8663)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.slf4j.LoggerFactory" on path: DexPathList[[dex file "/data/data/com.example.filme/code_cache/.overlay/base.apk/classes3.dex", dex file "/data/data/com.example.filme/code_cache/.overlay/base.apk/classes.dex", zip file "/data/app/~~SMMMBpkYv1Fu0fT2H1M_sA==/com.example.filme-ovE-fLeCY69AhQvQzyn1cA==/base.apk"],nativeLibraryDirectories=[/data/app/~~SMMMBpkYv1Fu0fT2H1M_sA==/com.example.filme-ovE-fLeCY69AhQvQzyn1cA==/lib/arm64, /system/lib64, /system/system_ext/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:218)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.hierynomus.smbj.SMBClient.<clinit>(SMBClient.java:119) 
at com.example.filme.MainActivity$1.onClick(MainActivity.java:36) 
at android.view.View.performClick(View.java:7792) 
at android.widget.TextView.performClick(TextView.java:16112) 
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
at android.view.View.performClickInternal(View.java:7769) 
at android.view.View.access$3800(View.java:910) 
at android.view.View$PerformClick.run(View.java:30213) 
at android.os.Handler.handleCallback(Handler.java:938) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loopOnce(Looper.java:226) 
at android.os.Looper.loop(Looper.java:313) 
at android.app.ActivityThread.main(ActivityThread.java:8663) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 
2022-04-25 11:45:27.518 22865-22865/com.example.filme I/Process: Sending signal. PID: 22865 SIG: 9
tried to install library slf4j-android-1.7.9.jar
but dont works for now.
Edit 12:46 o clock:
I added now many libraries and now its going close to work but not fully.
No library is missing but the following error occurs:
android.os.NetworkOnMainThreadException error
I read that I must do asynchronous processing.
But I'm too stupid to write this asynchronous code.. can anyone help?
Now its working.
I needed following LIBRARIES:
asn-one-0.6.0.jar
bcprov-jdk15on-1.70.jar
mbassador-1.3.2.jar
slf4j-api-1.7.9.jar
slf4j-nop-1.7.9.jar
smbj-0.11.5.jar

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();
}
}

Security Exception for Download Manager with targetSdkVersion Below 22

I have a weird crash in Lollipopand below. I get a security Exception when trying to download a file from the server, but in devices running Marshmallow and above the app does not crash. Logcat:
Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
In App's grandle I use
targetSdkVersion 22
Will change after our first push on Google Console to target the latest sdk version, but for now this will remain to 22, so no need for run time permissions.
Furthermore, in App's Manifest we declare app's permission.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Also, the code that uses the DownloadManagerinside an AsynckTask :
try {
String dirPath = String.format("%5$s/%1$s/Resources/%2$s/%3$s/%4$s/", userID, resource.getiD(), filePackage.getiD(), language, dir.getCanonicalPath());
File makeDirs = new File(dirPath);
makeDirs.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
this.downloadManager = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(url);
request.setDestinationInExternalFilesDir(getApplicationContext(), null, filePath);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setVisibleInDownloadsUi(false);
downloadID = downloadManager.enqueue(request);
Again, this crash is observed only in Galaxy A5 with OS version 5.0.2, in an emulator running Samsung Galaxy S6 with Lollipop and in emulator running Kitkat 4.4.4 HTC One.
The full stack trace :
java.lang.RuntimeException: Unable to start activity ComponentInfo{...players.PDFViewer}: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.os.Parcel.readException(Parcel.java:1540)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:475)
at android.content.ContentResolver.insert(ContentResolver.java:1260)
at android.app.DownloadManager.enqueue(DownloadManager.java:1336)
at de.imsystems.crmmobile.players.PDFViewer.onCreate(PDFViewer.java:104)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873) 
at android.app.ActivityThread.access$900(ActivityThread.java:181) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6145) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
If you have any thoughts, please share them, thanks in advance.
Eventually this post WRITE_EXTERNAL_STORAGE not working on lollipop even though it's set in the manifest helped me. What I did was to alter permissions in AndroidManifest.xml file like this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="26" tools:replace="android:maxSdkVersion"/>
This way the app doesn't crashes!

Using android internal storage for images

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" />`

Categories

Resources