尝试保存地图的屏幕截图时java.io.FileNotFoundException

时间:2014-09-15 09:16:03

标签: java android

当我尝试将screenShot保存到我手机内存中的 而不是SdCard 时,我收到此异常。 我检查了路径,这是正确的,我也将此权限定义到我的xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

screenShot的功能:

public void captureMapScreen() {
        SnapshotReadyCallback callback = new SnapshotReadyCallback() {
            Bitmap bitmap;
            @Override
            public void onSnapshotReady(Bitmap snapshot) {
                bitmap = snapshot;
                try {
                    // storage path (where image will be stored) + name of image
                    // you can customize as per
                    // your Requirement
                    FileOutputStream out = new FileOutputStream(Environment.getDataDirectory() + "/Download/" + "MyMapScreen" + System.currentTimeMillis()
                            + ".png");
                    bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        map.snapshot(callback);
    }

我的logcat:

09-15 11:02:09.323: W/System.err(26419): java.io.FileNotFoundException: /data/Download/MyMapScreen1410771729328.png: open failed: ENOENT (No such file or directory)
09-15 11:02:09.323: W/System.err(26419):    at libcore.io.IoBridge.open(IoBridge.java:416)
09-15 11:02:09.323: W/System.err(26419):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-15 11:02:09.323: W/System.err(26419):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
09-15 11:02:09.323: W/System.err(26419):    at com.logic.maps.activity.MapsActivity$1.onSnapshotReady(MapsActivity.java:317)
09-15 11:02:09.323: W/System.err(26419):    at com.google.android.gms.maps.GoogleMap$5.g(Unknown Source)
09-15 11:02:09.323: W/System.err(26419):    at com.google.android.gms.maps.internal.s$a.onTransact(Unknown Source)
09-15 11:02:09.323: W/System.err(26419):    at android.os.Binder.transact(Binder.java:326)
09-15 11:02:09.323: W/System.err(26419):    at gtu.a(SourceFile:117)
09-15 11:02:09.331: W/System.err(26419):    at ovg.a(Unknown Source)
09-15 11:02:09.331: W/System.err(26419):    at ovi.run(Unknown Source)
09-15 11:02:09.331: W/System.err(26419):    at android.os.Handler.handleCallback(Handler.java:615)
09-15 11:02:09.331: W/System.err(26419):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 11:02:09.331: W/System.err(26419):    at android.os.Looper.loop(Looper.java:137)
09-15 11:02:09.331: W/System.err(26419):    at android.app.ActivityThread.main(ActivityThread.java:4895)
09-15 11:02:09.331: W/System.err(26419):    at java.lang.reflect.Method.invokeNative(Native Method)
09-15 11:02:09.331: W/System.err(26419):    at java.lang.reflect.Method.invoke(Method.java:511)
09-15 11:02:09.331: W/System.err(26419):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
09-15 11:02:09.331: W/System.err(26419):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
09-15 11:02:09.331: W/System.err(26419):    at dalvik.system.NativeStart.main(Native Method)
09-15 11:02:09.331: W/System.err(26419): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-15 11:02:09.338: W/System.err(26419):    at libcore.io.Posix.open(Native Method)
09-15 11:02:09.338: W/System.err(26419):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-15 11:02:09.338: W/System.err(26419):    at libcore.io.IoBridge.open(IoBridge.java:400)
09-15 11:02:09.338: W/System.err(26419):    ... 18 more

更新

我的档案树:(手机内没有sdCard,只有手机内存)

storage
 - sdcard0
   - download

3 个答案:

答案 0 :(得分:1)

确保子目录Download存在于那里。

看到更新,您似乎想要存储到模拟的SD卡。请尝试Environment.getExternalStorageDirectory()并注意文件名区分大小写。

答案 1 :(得分:1)

Environment.getDataDirectory()并未指向sdcard,但它指向/data中的某个位置。

您需要使用Environment.getExternalStorageDirectory(),因为它指向制造商定义为外部存储路径的内容,即SD卡。

答案 2 :(得分:1)

尝试以下代码

  

文件storageDir =环境                       .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

     

file = new File(storageDir,&#34; MyMapScreen&#34; + System.currentTimeMillis()+&#34; .png&#34;);     FileOutputStream out = new FileOutputStream(file);