Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android save camera picture to local storage

Today I have to deal with a difficult thing.

I start the camera and want so save the taken image directly to my internal storage, without moving it into it.

    File targetDir = new File(getApplicationContext().getFilesDir()+File.separator+"PROJECTMAIN"+File.separator+"SUBFORDER");
    targetDir.mkdirs(); //create the folder if they don't exist

    File externalFile = new File(targetDir, "picturename.jpg");
    Uri imageURI = Uri.fromFile(externalFile);

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageURI);
    startActivityForResult(takePictureIntent, actionCode);

It seems that if I try to save them directly into the internal storage, the camera ignores my click on the "ok"-button after I take the picture. I think there is something wrong with the "internal" URI, because if I use Environment.getExternalStorageDirectory() instead of getApplicationContext().getFilesDir() for extra_output, everything works fine, but then I have to move the file afterwards into the internal storage (the movement process works fine to "getApplicationContext().getFilesDir()")

The camera just doesn't do anything when I take a picture and press the ok button to continue with the internal URI... I can't believe that is that difficult with storage in Android.

Any ideas? maybe the camera just allows to save pictures to the external storage?

like image 228
Niko Avatar asked Oct 21 '22 14:10

Niko


1 Answers

Try following code

File dir= context.getDir("dirname", Context.MODE_PRIVATE); //Creates Dir inside internal memory
File file= new File(dir, "filename");  //It has directory details and file name
FileOutputStream fos = new FileOutputStream(file);
like image 152
Aju Avatar answered Oct 29 '22 19:10

Aju