Save captured image in specific folder on sd card

Tashen Jazbi picture Tashen Jazbi · Aug 11, 2014 · Viewed 8.5k times · Source

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 :)

Answer

ASP picture ASP · Aug 11, 2014

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"/>