2017-01-19 25 views
3

我的应用程序应该在external storage上保存一个piechart作为png。但是出现这样的错误:没有明显的原因找不到文件错误?

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

我跟着指示添加这种功能非常密切(从这个tutorial),但还没有出现错误。该应用程序有权写入外部存储,我在我的android_manifest.xml中添加了权限。

你们可以发现错误吗?因为我不可以。

如果你还找不到错误,你能推荐任何其他方式来做到这一点?

即时通讯使用MPAndroidChart,但我真的不认为它与此有关,因为我可以尝试保存任何其他对象,错误依然存在。

代码

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

完整的错误

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) 
+0

检查,如果你有路径和名称是否正确的确切情况下,Android的,是基于Linux上,有一个区分大小写的文件系统。 – john16384

+0

您是否有存储权限?外部存储可能需要许可。 – Dawnkeeper

+0

我有@dawnkeeper,在我的android_manifest.xml中添加了权限 – niklasdude

回答