Kæmpe Klunker
Kæmpe Klunker

Reputation: 865

Camera not saving image

Can someone tell me why this code is not saving the picture to the gallery anymore? I had it working at some point, then i changed something somewhere else, and now this doesn't work.

private void openImageIntent() {

    // Determine Uri of camera image to save.
    final File storageDir = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    final String fname = ClassName.getUniqueImageFilename();
    final File sdImageMainDirectory = new File(storageDir, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
            captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName,
                res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
    }

    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent,
            "Chose a source");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[] {}));

    startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}

private static String getUniqueImageFilename() {
    // TODO Auto-generated method stub
    String fileName = "img_" + System.currentTimeMillis() + ".jpg";
    return fileName;
}

This is the error i get:

10-29 23:53:50.692: E/BitmapFactory(12547): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/DCIM/img_1414623222659.jpg: open failed: ENOENT (No such file or directory)
10-29 23:53:50.692: E/BitmapFactory(12547): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/DCIM/img_1414623222659.jpg: open failed: ENOENT (No such file or directory)

Upvotes: 0

Views: 349

Answers (1)

user4196638
user4196638

Reputation:

final File storageDir = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

You should put permission in Android Manifest to write to External Storage

Android storage is broken over different version of Android SDK. Your code will work on some devices but not all.

The System cannot create a file on system because of lack of permission so it is giving you error. The best way I have found is just to see that if external storage is mounted then create a file on external.

There are following two code snippets and they both will work.

Environment.getExternalStorageDirectory() + "/" + "myImages" + "/someimage.jpg";

After that you can start Media Store update Intent to update Android Gallery.

or Use this code

final File dir = new File(context.getFilesDir() + "/nfs/guille/groce/users/nicholsk/workspace3/SQLTest");
dir.mkdirs(); //create folders where write files
final File file = new File(dir, "BlockForTest.txt");

Upvotes: 2

Related Questions