5

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?

Perception
  • 79,279
  • 19
  • 185
  • 195
Niko
  • 1,054
  • 5
  • 25
  • 52

2 Answers2

3

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);
Aju
  • 4,597
  • 7
  • 35
  • 58
0

For 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 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 for the camera intent use this,

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);
        }
Ajith K P
  • 398
  • 3
  • 12