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)