1

I have to make some changes to one of my apps to support Android 7. I'm creating an ACTION_IMAGE_CAPTURE - Intent with a content Uri for EXTRA_OUTPUT, and I want to set the filename that is used for the image. Is there a way to do this by setting a certain entry for ContentValues ? The Uri I get from back from

getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

contains the MediaStore ID of the file. I look up the real image path at a later point, but I want to define the filename before the Intent is processed.

Code for creating the intent:

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    ContentValues values = new ContentValues(3);

    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    docUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, docUri);
    startActivityForResult(takePictureIntent, PICK_PHOTO_CODE);

UPDATE: Final code, with setting the filename and date taken of the photo:

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    ContentValues values = new ContentValues(3);
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
    values.put(MediaStore.Images.Media.DATE_TAKEN, new Date().getTime());

    docUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, docUri);
    startActivityForResult(takePictureIntent, PICK_PHOTO_CODE);
Dave
  • 81
  • 5
  • Have `EXTRA_OUTPUT` point to a file under your control (e.g., one from `FileProvider`), so you can control the name, rather than asking `MediaStore` to generate a random name and random location for the image. – CommonsWare Nov 01 '17 at 14:27
  • FileProvider does not work with files on an sdcard, which is often the storage place for pictures in my use case. – Dave Nov 01 '17 at 14:33
  • "FileProvider does not work with files on an sdcard" -- if by "sdcard" you mean removable storage, you are correct. However, there is no guarantee that `MediaStore` will put the image on removable storage either. By using `insert()`, you are saying that you do not care where the image gets stored. – CommonsWare Nov 01 '17 at 14:43
  • I have figured it out. You can set the file path with ContenValues: values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); – Dave Nov 01 '17 at 14:46
  • That is quite interesting code. Thanks. Even a path to the sd card where the app cannot write itself is ok. Worked on Android 6. Not on 7. – greenapps Nov 01 '17 at 15:51
  • Strange, it works on 6 and 7 for me. I have updated my post with the final solution. Thanks guys. – Dave Nov 09 '17 at 09:41
  • How do you set the custom location? – Kellin Strook Jul 13 '20 at 11:09

1 Answers1

1

I've found that by setting the DISPLAY_NAME the file path is still managed by ContentProvider but uses the provided name for the file (with .jpg appended).

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DISPLAY_NAME, "name_for_file");
docUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Eerko
  • 136
  • 2
  • 6