user3195396
user3195396

Reputation: 254

Android: How do I save captured image(s) to a specific folder

I'm a total newbie in Android programming and I'm having trouble figuring how to save an image to a specific folder. Let's say i have a folder called "myCapturedImages" and I would to save it in this folder/directory. The directory is located at Internal Storage\Pictures\myCapturedImages. The pictures that I took has its size of 0 bytes and cannot be opened. I think the main problem is my onPictureTaken function. Any clue on how I should achieve the expected save directory?

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        /*Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */

        Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());

        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriTarget);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(VuzixCamera.this, "Image saved: " + uriTarget.toString(), Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        camera.startPreview();
    }};

Upvotes: 1

Views: 6787

Answers (2)

Bhaskar
Bhaskar

Reputation: 949

this is simple program to launch camera and save picture to specific folder...

File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

Make sure that you have added WRITE_EXTERNAL STORAGE and CAMERA permissions in manifest file.

Upvotes: 1

Rajesh Mikkilineni
Rajesh Mikkilineni

Reputation: 844

get the Bitmap of the image and For saving Image you can use this code

//showedImgae is your Bitmap image

public void SaveImage(Bitmap showedImgae){

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/DCIM/myCapturedImages");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "FILENAME-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream out = new FileOutputStream(file);
        showedImgae.compress(Bitmap.CompressFormat.JPEG, 100, out);
        Toast.makeText(activityname.this, "Image Saved", Toast.LENGTH_SHORT).show();
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    getApplicationContext().sendBroadcast(mediaScanIntent);
}

Upvotes: 1

Related Questions