【问题标题】:Android canvas save always java.io.IOException: open failed: ENOENT (No such file or directory)Android画布总是保存java.io.IOException:打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2013-09-04 04:31:14
【问题描述】:

我有一个画布应用程序。我正在尝试使用Canvas + onTouchListener 创建一个签名应用程序。

这是我的保存方法,我尝试将签名保存到图像中:

private void save() {
    hideMenuBar();
    View content = this;
    content.setDrawingCacheEnabled(true);
    content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    Bitmap bitmap = content.getDrawingCache();
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    String imgPath = path+"/imotax/capture/spop/ttd/image" + "temp" + ".jpg";
    File file = new File(imgPath);
    FileOutputStream ostream;
    try {
        file.createNewFile();
        ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.JPEG, 100, ostream);
        ostream.flush();
        ostream.close();
        Toast.makeText(getContext(), "image saved", 5000).show();
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("ttd", e.toString());
        Toast.makeText(getContext(), "Failed To Save", 5000).show();
        showMenuBar();
    }
}

不知道为什么,总是出错或者进入catch语句出现这个错误:

java.io.IOException: open failed: ENOENT (No such file or directory)

【问题讨论】:

    标签: android android-canvas


    【解决方案1】:

    问题是您错过了 sdcard0

    之间的“/”
    storage/sdcard0/imotax/capture/spop/ttd/image/tmp.png
    should be
    storage/sdcard/0/imotax/capture/spop/ttd/image/tmp.png
    

    【讨论】:

      【解决方案2】:

      试试这个方法

      private void save() {
              try {
                  hideMenuBar();
                  View content = this;
                  content.setDrawingCacheEnabled(true);
                  content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                  Bitmap bitmap = content.getDrawingCache();
      
                  String extr = Environment.getExternalStorageDirectory().toString();
                  File mFolder = new File(extr + "/imotax/capture/spop/ttd/image");
                  if (!mFolder.exists()) {
                      mFolder.mkdir();
                  }
      
                  String s = "tmp.png";
      
                  File f = new File(mFolder.getAbsolutePath(), s);
      
                  FileOutputStream fos = null;
                  fos = new FileOutputStream(f);
                  bitmap.compress(CompressFormat.JPEG, 100, fos);
                  fos.flush();
                  fos.close();
      
                  bitmap.recycle();
      
                  Toast.makeText(getContext(), "image saved", 5000).show();
              } catch (Exception e) {
                  Toast.makeText(getContext(), "Failed To Save", 5000).show();
              }
          }
      

      更新

      File mFolder = new File(extr + "/imotax/capture/spop/ttd/image");  //replace with
      
      File mFolder = new File(extr + "/imotax");
      

      【讨论】:

      • 在设备或模拟器上运行?
      • 设备这是错误:09-04 12:28:07.863: I/ttd(16524): java.io.FileNotFoundException: /storage/sdcard0/imotax/capture/spop/ttd/image/ tmp.png:打开失败:ENOENT(没有这样的文件或目录)
      • 嘿,它可以保存图像,非常感谢:),但为什么只有黑色? :(
      • 移除这一行 content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);并尝试
      • 我仍然面临问题。 /storage/emulated/0/Download/tmp.png:打开失败:EACCES(权限被拒绝)
      【解决方案3】:
      Add this permissions in manifest.
      
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      

      【讨论】:

      【解决方案4】:

      不要问我为什么,但这似乎是访问权限的问题。尝试使用一些公共目录。使用类似的东西:

      Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多