3

My application should save a piechart as a png on the external storage. However this error appears:

java.io.FileNotFoundException: /storage/emulated/0/SAVE IMAGE EXAMPLE/myimage.png (No such file or directory)

I followed the instructions to add this kind of functionality very closely (From this tutorial), but yet the error appears. The app has the permission to write to external storage, I added the permission in my android_manifest.xml.

Can you guys spot the error? Because i can not.

If you also can't find the error, can you recommend any other way to do this?

Im using MPAndroidChart, but i don't really think it has to do with this, because i could try to save any other object and the error remains.

The code is

final PieChart piechart = (PieChart) findViewById(R.id.piechart);  

button2.setOnClickListener(new View.OnClickListener() {
  public void onClick (View v) {
    Toast.makeText(Main.this, "Chart Saved", Toast.LENGTH_SHORT).show();
    piechart.setCenterText("Test");
    Bitmap bitmap;
    OutputStream output;

    bitmap = BitmapFactory.decodeResource(getResources(),R.id.piechart);

    File filepath = Environment.getExternalStorageDirectory();

    File dir = new File(filepath.getAbsolutePath()+"/SAVE IMAGE EXAMPLE");
    dir.mkdirs();

    File file = new File(dir, "myimage.png");

    try {
       output = new FileOutputStream(file);

       bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
       output.flush();
       output.close();


    }catch(Exception e){
       e.printStackTrace();
    }
 } 
});

FULL ERROR

    W/System.err: java.io.FileNotFoundException: /storage/emulated/0/SAVE IMAGE EXAMPLE/myimage.png (No such file or directory)
W/System.err:     at java.io.FileOutputStream.open(Native Method)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
W/System.err:     at com.pies.quickpie.Main$1$3.onClick(Main.java:175)
W/System.err:     at android.view.View.performClick(View.java:5637)
W/System.err:     at android.view.View$PerformClick.run(View.java:22429)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
niklasdude
  • 43
  • 5
  • Check if you have the exact case of the path and name correct, Android, being based on Linux, has a case sensitive file system. – john16384 Jan 19 '17 at 15:07
  • Do you have access rights to the storage. External storage might require a permission. – Dawnkeeper Jan 19 '17 at 15:09
  • i have @dawnkeeper , added the permission in my android_manifest.xml – niklasdude Jan 19 '17 at 15:13
  • `can you elaborate what that axactly means @john16384 ? sorry, i'm new – niklasdude Jan 19 '17 at 15:14
  • Check if `dir.mkdirs();` actually returns `true` -- if it doesn't, the dirs will not have been created. – john16384 Jan 19 '17 at 15:21
  • what is the android version on which you are testing this? i doubt it's a runtime permission model issue – Pavneet_Singh Jan 19 '17 at 15:22
  • `dir.mkdirs();`. Only call mkdirs() if the directory does not exist with dir.exists(). Then check the return value!. This has been said before. If the directory cannot be created and hence not exists then display a toast to the user saying so. And return. Dont continue with your code then as it makes little sense trying to create a file in a directory that does not exist. – greenapps Jan 19 '17 at 15:26
  • @PavneetSingh I'm running it on a virtual device with android nougat – niklasdude Jan 19 '17 at 15:35

1 Answers1

0

You need to implement Runtime Permission model to get Storage Permission from user from marshmallow and above.

See the example here for storage permission

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68