I am trying to click a image using camera of phone and trying to save the image in a separate directory in sdcard. I have tried many methods and I always get fileNotFoundException. I have both the following permissions defined in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
Below is the code which is giving me error:
private void captureImage() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
My onActivityResult method:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
if (data != null) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
postImage.setImageBitmap(photo); /* this is image view where you want to set image*/
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
int count1 = count + 1;
createDirectoryAndSaveFile(photo, "" + userId + "_" + System.currentTimeMillis() + "mobileImageCapture" + count1);
}
}
}
Create directory and save file method:
private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {
File direct = new File(Environment.getExternalStorageDirectory() + "/Toadways");
if (!direct.exists()) {
File wallpaperDirectory = new File("/sdcard/Toadways/");
wallpaperDirectory.mkdirs();
}
File file = new File(new File("/sdcard/Toadways/"), fileName);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
My Crash Log:
W/System.err: java.io.FileNotFoundException: /sdcard/DirName/56332edfad441746cbd15000_1451985027984mobile
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
W/System.err: at toadways.ways.toad.toadways.ISayIPoll.createDirectoryAndSaveFile(ISayIPoll.java:1095
W/System.err: at toadways.ways.toad.toadways.ISayIPoll.onActivityResult(ISayIPoll.java:779)
W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:6456)
W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
W/System.err: at android.app.ActivityThread.-wrap16(ActivityThread.java)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
W/System.err: ... 15 more
My Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
......
</application>
</manifest>
My manifest file is too big, so I am posting part of it, which is needed.