【问题标题】:File not found error for no apparent reason?没有明显原因的文件未找到错误?
【发布时间】:2017-06-04 08:19:52
【问题描述】:

我的应用程序应该将piechart 保存为external storage 上的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)

【问题讨论】:

  • 检查路径和名称的大小写是否正确,基于 Linux 的 Android 具有区分大小写的文件系统。
  • 您是否有权访问存储。外部存储可能需要权限。
  • 我有 @dawnkeeper ,在我的 android_manifest.xml 中添加了权限
  • `你能详细说明一下@john16384 的确切含义吗?对不起,我是新人
  • 检查dir.mkdirs();是否真的返回true——如果没有,目录就不会被创建。

标签: java android image save storage


【解决方案1】:

您需要实现Runtime Permission model 以获得来自棉花糖及以上用户的Storage 权限。

See the example here for storage permission

【讨论】:

  • 谢谢,我试试这个!
猜你喜欢
  • 2022-11-02
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2014-08-05
相关资源
最近更新 更多