Tashen Jazbi
Tashen Jazbi

Reputation: 1068

Save captured image in specific folder on sd card

I'm working with camera in android and I'm new to android camera. I've followed the tutorial from here

Whenever I'll add External storage permission in android manifest, camera saves it in default directory without asking me and I want to save it in my own folder created in sd card. I'd searched a lot but couldn't find any useful link. Please help, any help will be much appreciated. Thank you :)

Upvotes: 6

Views: 8593

Answers (3)

Nish8900
Nish8900

Reputation: 82

You can assign a path to write the image data to specific location like this:

String fileName = "/mnt/sdcard/foldername";
FileOutputStream  fos = new FileOutputStream(fileName);
fos.write(data);

Upvotes: 0

ASP
ASP

Reputation: 1974

You can add this code in onActivityResult. This will store your image in a folder named "YourFolderName"

String extr = Environment.getExternalStorageDirectory().toString()
            + File.separator + "YourFolderName";
    File myPath = new File(extr, fileName);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(myPath);
        bitMap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(context.getContentResolver(),
                bitMap, myPath.getPath(), fileName);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

also need to set permission on Manifest

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

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1006564

camera saves it in default directory without asking me and I want to save it in my own folder created in sd card

You can tell the camera where you would like the photo to be stored via EXTRA_OUTPUT, as is shown in the documentation's training module on ACTION_IMAGE_CAPTURE.

There is no requirement for all cameras to store images where you ask it to, though. If the file that you ask for does not exist after the image is taken, you will need to fall back to your existing application logic.

Upvotes: 1

Related Questions