9

Suppose I am writing an alternative Camera application and wish to write images exactly into the same place as Camera does and name them exactly in the same name Camera does.

How would I accomplish this?

How to know the location of camera files?

How to know current naming convention?

How to gain permissions to that directory?

Any of answer would be appreciated.


Okay, suppose it is not really camera alternative. Suppose I would like to write formats other than images, like audio, video, or something else.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Dims
  • 47,675
  • 117
  • 331
  • 600
  • 1
    A couple things: You have a very open ended question. It contains multiple questions instead of a single one that can be answered. There are lots of guides on the internet for more of those tasks—which ones have you tried and are having problems with? And questions for suggestions on libraries or tutorials are discouraged on this site since they can't be answered definitively. – Jon Adams Jan 21 '17 at 17:42
  • 9
    There are ~2 billion Android devices, from thousands of models. There are hundreds of different camera apps that ship by default on those models, and there are hundreds of additional camera apps available for download from the Play Store and elsewhere. There is no single "Camera application". You are welcome to use `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)` to get the general location, but there is no requirement for any given camera app to put its photos there or name them with some standard format. – CommonsWare Jan 21 '17 at 18:28
  • 2
    You can learn a lot from OpenCamera project on GitHub – Alex Cohn Jan 21 '17 at 18:41
  • @CommonsWare you are wrong. If you connect your device to big computer with `USB` and will enable file access, you will see the VERY ONE `DCIM` folder. All other folders will be burried deep below in the filesystem. This is special place this is THE ONE place. I wan't my application write namely here. Not into billions possible places you mention. – Dims Jan 21 '17 at 19:47
  • 5
    "you will see the VERY ONE DCIM folder" -- correct. That is `Environment.getExternalStoragePublicDirectory(Environment.DI‌​RECTORY_DCIM)`. Whether any given camera app writes to that directory, a subdirectory of `DCIM`, or somewhere else, is up to the developers of the camera app. "I wan't my application write namely here" -- not according to your question, which has "wish to write images exactly into the same place as Camera does and name them exactly in the same name Camera does", for some unidentified "Camera" app. – CommonsWare Jan 21 '17 at 19:53
  • Will my application has permissions to write to `Environment.getExternalStoragePublicDirectory(Environment.DI‌​‌​RECTORY_DCIM)` by default? – Dims Jan 21 '17 at 19:55
  • No, you need `` for `WRITE_EXTERNAL_STORAGE`, including adding support for runtime permissions if your `targetSdkVersion` is 23 or higher. – CommonsWare Jan 22 '17 at 13:51
  • It sounds like your real question is how to write files to a removable storage device. You need to use the Storage Access Framework (SAF). See my answer here for more info: http://stackoverflow.com/a/35175460/1048340 – Jared Rummler Feb 11 '17 at 10:40
  • @Dims, you wrote: "How to gain permissions to that directory?" See https://stackoverflow.com/a/33031091/966789 (Android added new permission model for Android 6.0 Marshmallow). – Alexander Lubyagin Dec 11 '17 at 07:16

5 Answers5

6

How would I accomplish this?

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "You Dir. Name";

if you append any string to end this

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

it will create dir inside the folder

How to know the location of camera files?

By default camera uses this location

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

How to know current naming convention?

IMG_yyyyMMDD_timeStamp

How to gain permissions to that directory?

Using permission manager for Camera and External Storage permissions

Hemanth S
  • 669
  • 6
  • 16
5

You cannot write exactly into the same folder as the default camera app does. But you can make use of Environment.getExternalStoragePublicDirectory(Environment.DI‌​‌RECTORY_DCIM)


mediaStorageDir = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
     Uri.fromFile(mediaStorageDir));
startActivityForResult(takePictureFromCameraIntent, 100);

But, please note that this will create only a sub-folder in the DCIM directory and will not store exactly where the default camera does. But you can always create sub-folders with your required folder name.

Vishnu
  • 663
  • 3
  • 7
  • 24
Sanal Varghese
  • 1,465
  • 4
  • 23
  • 46
  • Firs, your suggestion only works if Camera is set up to write to internal storage. If it is set up to write to SD card, your path will be irrelevant. Second, I don't need to create folders, I need to write exactly the same place as camera does. This is the question. – Dims Feb 09 '17 at 08:51
  • 1
    As far as I know, you can't write to exactly where the camera does. – Sanal Varghese Feb 09 '17 at 10:40
1

Things to keep in mind for this answer:

  • Every phone producer creates it's own Camera App, tailored to their hardware.

  • With the right permissions, App's can write (almost) everywhere they want...

Now to your question(s):

First, we don't know where the photo's are stored, and what the naming convention is. Every one and their mother has a different idea about what is best. So no "Hey it's always there".

Those who seek will find: get read & write permissions for the whole device. With that search the whole device to find in what folders the images are in. Now subtract the folders that come from "social media". My guess would be that the folder with the most and or latest images is the one that you want. To be sure, you will need testers, that trust you.

All that are found were not unorganized: just find the pattern used. There may be a standard for this. The big companies will surely have one. You can ask the device what maker it has. Note, that that answer might not be correct.

Ain't that a picture, no, it's a photo: And then you get the fun part of accessing the camera. Good times. Remember: request picture => get location of picture in memory => save picture as file. Best part, there is no enforcement of all parts of this API, so different devices need different instructions...

Have fun & Good luck!

ps, the downvotes are probably for the lack of code

Community
  • 1
  • 1
  • No, downvotes are from proponents of unfail competition. They displike if people want to do their applications allowed to do – Dims Feb 10 '17 at 11:20
0

Try using this code you can save the image bitmap to the directory using insert query

String imgSaved = MediaStore.Images.Media.insertImage(
                getContentResolver(), bitmap,
                System.currentTimeMillis() + ".jpg", "DESCRIPTION HERE");

For more details see the link

Anuj J Pandey
  • 656
  • 1
  • 4
  • 17
  • Try Using FileProvider for storing image anywhere you want from android n onwards [Fileprovider] https://developer.android.com/reference/android/support/v4/content/FileProvider.html – Vinayagam.D Feb 10 '17 at 07:27
  • @AnujJPandey your code writes to `/storage/emulated/0/Pictures/`, while I need to write to `/storage/9C33-6BBD/DCIM/Camera` – Dims Feb 10 '17 at 11:16
  • ContentValues values = new ContentValues(); values.put(MediaStore.Images.ImageColumns.DATA, data); Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); – Anuj J Pandey Feb 10 '17 at 13:19
0

I've found this code useful for choosing the last used DCIM/Camera folder.

String getDCIMCamera() {
    try {
        String[] projection = new String[]{
                MediaStore.Images.ImageColumns._ID,
                MediaStore.Images.ImageColumns.DATA,
                MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
                MediaStore.Images.ImageColumns.DATE_TAKEN,
                MediaStore.Images.ImageColumns.MIME_TYPE};

        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                null,
                null,
                MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
        if (cursor != null) {
            cursor.moveToFirst();
            do {
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                if (path.contains("/DCIM/")) {
                    File file = new File(path);
                    path = file.getParent();
                    cursor.close();
                    return path;
                }
            } while (cursor.moveToNext());
            cursor.close();
        }
    }
    catch (Exception e) {
    }
    return "";
}
Alexander Lubyagin
  • 1,346
  • 1
  • 16
  • 30