OiRc
OiRc

Reputation: 1622

java.io.FileNotFoundException when trying to save a map's screenshot

i' m getting this exception when i try to save the screenShot into my download folder inside my phone's memory not the SdCard. I checked the path and it's correct and i have also this permission defined into my xml:

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

screenShot's function:

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

my 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

UPDATE:

my archive tree: (no sdCard inside my phone, only phone memory)

storage
 - sdcard0
   - download

Upvotes: 0

Views: 1359

Answers (3)

Pr38y
Pr38y

Reputation: 1565

Try the following code

File storageDir = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

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

Upvotes: 1

Cheesebaron
Cheesebaron

Reputation: 24470

Environment.getDataDirectory() does not point to the sdcard, however it points to somewhere in /data.

You need to use Environment.getExternalStorageDirectory() instead, as it points to what the manufacturor has defined as the path for the external storage, i.e. sdcard.

Upvotes: 1

Henry
Henry

Reputation: 43798

Make sure that the subdirectory Download exists there.

Seeing the update, it seems you want to store to an emulated SD card. Try Environment.getExternalStorageDirectory() and also note that file names are case sensitive.

Upvotes: 1

Related Questions