How to write file into DCIM directory exactly wher

2019-03-07 04:19发布

问题:

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.

UPDATE

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

UPDATE 2

Please explain, why the hell you are downvoting??

UPDATE 3

Time came to try all advices and I found they all incorrect. Just incredible how many legends walking here on Stackoverflow...

回答1:

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

mediaStorageDir = newFile(Environment.getExternalStorageDirectory(),FOLDER_NAME)
Intent takePictureFromCameraIntent = newIntent(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 DCIM directory and will not store exactly where default camera stores. But you can always create sub folders with any your required folder name.

`



回答2:

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



回答3:

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



回答4:

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



回答5:

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 "";
}