Android saves camera image to local storage

Today I have to deal with a difficult task.

I start the camera and want to save the saved image directly to my internal storage without moving it to 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 to the internal storage, the camera will ignore my click on the ok button after I take the picture. I think something is wrong with the โ€œinternalโ€ URI, because if I use extra_output Environment.getExternalStorageDirectory()instead getApplicationContext().getFilesDir(), everything works fine, but then I need to move the file to internal memory (the move process works fine to "getApplicationContext (). GetFilesDir () ")

The camera just does nothing when I take a picture and press the ok button to continue the internal URI ... I cannot believe that it is difficult with storage on Android.

Any ideas? Perhaps the camera just allows you to save pictures in external storage?

+5
source share
2 answers

Try the 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);
+3
source

For a higher version of Android 7.0 use this code,

<application
    ...>
    <activity>
    .... 
    </activity>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.your.package.fileProvider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
  </application>

now create a file in the xml resource folder,

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-path path="Android/data/com.your.package/" name="files_root" />
  <external-path path="." name="external_storage_root" />
</paths>

then whenever use to use the camera uses it,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(getContext(), "com.your.package.fileProvider", newFile);
            intent.setDataAndType(contentUri, type);
        } else {
            intent.setDataAndType(Uri.fromFile(newFile), type);
        }
0
source

All Articles