0

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.

  1. Compile and run the project with file insertions and check.

  2. 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??

Hyun I Kim
  • 589
  • 1
  • 3
  • 14

2 Answers2

0

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" />`
sanjeev kumar
  • 541
  • 4
  • 20
0

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;

  1. Put READ/WRITE Storage permissions in your Manifest.
  2. If your Android Version is Marshmallow or greater, ask run-time permissions. You can get information about this here
  3. Make sure the path is correct. Most of the time hard-coded paths make difference. Refer here for more detail.
  4. Make sure you call mkdirs() if directory doesn't exist.
  5. 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.
Community
  • 1
  • 1
Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
  • Thanks for the reply, but I am using internal storage and it doesn't require permission. I don't hard-code the path but get it from ContextWrapper. I check if the folder exists inside the if statement in saveBitmapWithThumbnail() method. 11.png and 11_tn.png were just examples and I use file names according to sqllite's id which starts from 1. That's why my application tried to access 1.png and 1_tn.png. They were ther first to put inside the database and id was set to 1. – Hyun I Kim Apr 06 '17 at 06:38
  • Your `error` clearly says it doesn't find the file, so there must be something wrong with the storage. Take a look [here](http://stackoverflow.com/questions/26579869/how-can-i-let-users-access-the-internal-storage-directory-of-my-app) – Waqas Ahmed Ansari Apr 06 '17 at 07:00
  • Yeah... That's why I am so confused.. I saw that question when I searched this problem but he was confusing internal and external storage path while I am using only internal path that ContextWrapper and File provides. Also, it is far more weird because my code works when it's first executed. It can write and read the images. Error occurs when I close my application and re run it.. I have no idea what's goin on. :( – Hyun I Kim Apr 06 '17 at 07:29