vandus
vandus

Reputation: 3288

How to take a picture and save it in a custom folder for my app visible in the Gallery

I´ve been struggling with this for a few days now and other answers posted in similar questions here on stackoverflow haven´t helped me.

What I want to do is set a custom ArrayAdapter to my ListView and inside this adapter I want to set an onClickListener to a button that appears in every item. Then I want the user to pick whether he wants to take a picture with the camera or choose a picture from the gallery. Then I want the picture to save in the app´s own folder inside Gallery. However, although the custom folder is created and visible in Gallery, the picture itself is stored in the Camera folder and I can see a broken file in the custom folder.

I´ve read photobasics on the devsite http://developer.android.com/training/camera/photobasics.html, but it did not help much. I implemented the onActivityResult inside my Fragment but the Uri path is different fro the one created in the adapter.

Here is the code:

The path I add to the intent is file:/storage/emulated/0/Pictures/MyApp/JPEG_20140626_133228_1332202116.jpg but suddenly changes to content://media/external/images/media/6273 in the Intent return by onActivityResult.

Where am I going wrong?

Upvotes: 1

Views: 4110

Answers (2)

Agostinhob07
Agostinhob07

Reputation: 87

You have a really good example here -> Taking Photos Simply.

When you go save with createImageFile function you can choose your directory url:

private File createImageFile() throws IOException {
// Create an image file name
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);]
File image = File.createTempFile(
    imageFileName,
    ".jpg",
    storageDir
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}

I think this is what you need!

Upvotes: 0

SHIDHIN TS
SHIDHIN TS

Reputation: 1665

Here is the function to save the Image,

public static String saveImageInExternalCacheDir(Context context, Bitmap bitmap, String myfileName) {
    String fileName = myfileName.replace(' ', '_') + getCurrentDate().toString().replace(' ', '_').replace(":", "_");
    String filePath = (context.getExternalCacheDir()).toString() + "/" + fileName + ".jpg";
    try {
        FileOutputStream fos = new FileOutputStream(new File(filePath));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return filePath;
}

Upvotes: 1

Related Questions