3

Possible Duplicate:
How to get camera result as a uri in data folder?

I'm taking a Picure, and saving it in the default folder.

Here is my PictureCallBack() function:

PictureCallback myPictureCallback_JPG = new PictureCallback(){

        public void onPictureTaken(byte[] arg0, Camera arg1) {       

          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(MainActivity.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();
         }
    };

How can I save it in a specific folder like "/sdcard/MyCustonApp/Photos/"?

Community
  • 1
  • 1
Bruno Almeida
  • 166
  • 3
  • 14

4 Answers4

4

Add to your code

    String path = Environment.getExternalStorageDirectory().toString();
    File myNewFolder = new File(path + "/your/folder");
    myNewFolder.mkdir();
    File file = new File(path + "/your/folder", name    + ".png");
    OutputStream imageFileOS = new FileOutputStream(file);
prozhyga
  • 439
  • 2
  • 7
  • 1
    Nice, but now the picture doesn't show in the gallery or take too long to show. Any idea what I did wrong? – Bruno Almeida Nov 23 '12 at 15:38
  • 1
    To show picture in gallery you must add file with image to gallery: MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); – prozhyga Nov 26 '12 at 12:58
  • How did you solve it ? may ypu copy all your code here !?! – محمد Jan 27 '15 at 12:45
1

This is how I check if the folder I want to save the captured image to, exists, or create the folder if it doesn't.

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

File cameraFolder;

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"MyCustomApp/Photos");
else
    cameraFolder = StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
    cameraFolder.mkdirs();

File photo = new File(Environment.getExternalStorageDirectory(), "MyCustomApp/Photos/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Uri uri = Uri.fromFile(photo);

startActivityForResult(getCameraImage, 1);

This piece of code will save the captured image to the specified folder with the name camera_snap and extension: .jpg

NOTE: This requires you to declare the following permission in your Manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
1
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); 
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

Have a look at this link. How to save images from Camera in Android to specific folder?.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

try this simple code and store images in one folder:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

where bm means bitmap image name

SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23