Android device specific camera path issue

2019-01-18 04:10发布

问题:

I know that my question is a duplicate of this question and this one. But still I have some problems, so don't try to close or downvote my question friends. Please go through the question fully.

In my app I want to store Images into the same folder as where the device native Camera stores them. As per my research, I have found out that each device stores the Images into a different name and in different folder.

That is:

  • Nexus-One stores its camera files into a folder named Camera (/sdcard/DCIM/Camera).

  • All HTC devices store their camera files into a folder named 100MEDIA (/sdcard/DCIM/100MEDIA).

  • Sony Xperia x10 stores its camera files into a folder named 100ANDRO (/sdcard/DCIM/100ANDRO).

  • Motorola MilesStone stores its camera files into a folder named Camera (/sdcard/DCIM/Camera).

So I wanted to know if it's programmatically possible to get this path so that I can store the images taken from my app to the same location?

When I was googling I found out that it's better to create an external folder of my own so that I can store the images in that path with the name I am specifying. After that also in HTC device of API version 3 (Android 1.5) I found out that only the folder is getting created but the image gets saved in its default place with a name of its own.

How to solve this issue? Is it not possible to find the specific path and name of the image that gets saved for each device? Please help me friends.

回答1:

Use getExternalStorageDirectory() if API level is below 7 and then append /Pictures to get the path of Photos storage.

for API level > 7 use getExternalStoragePublicDirectory (DIRECTORY_PICTURES).



回答2:

I use the following code

String pictures_path = Utils.getRealPathFromURI(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
File path = new File(pictures_path);
if (path.isFile())
    path = path.getParentFile();

Where Utils:

public static String getRealPathFromURI(Uri content_uri, int media_type) {
    String column = MediaType.MEDIA_TYPE_PICTURE;       
    ContentResolver content_resolver = getContext().getContentResolver();
    String [] proj = { column };
    Cursor cursor = content_resolver.query(content_uri,
                    proj, // Which columns to return
                    null, // WHERE clause; which rows to return (all rows)
                    null, // WHERE clause selection arguments (none)
                    null); // Order-by clause (ascending by name)
    int column_index = cursor.getColumnIndexOrThrow(column);
    if (!cursor.moveToFirst())
        return null;
    return cursor.getString(column_index);
}

EDIT: Unfortunatelly, the approach above may not always work :( ...

Eventually I did manual checkings:

    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    if (path.exists()) {
        File test1 = new File(path, "100MEDIA/");
        if (test1.exists()) {
            path = test1;
        } else {
            File test2 = new File(path, "100ANDRO/");
            if (test2.exists()) {
                path = test2;
            } else {
                File test3 = new File(path, "Camera/");
                if (!test3.exists()) {
                    test3.mkdirs();
                }
                path = test3;
            }
        }
    } else {
        path = new File(path, "Camera/");
        path.mkdirs();
    }


回答3:

This is the function that I am using. It doesnt specifically name 100Media or Camera directories. It simply starts in the /DCIM folder and looks at its children. If the child is not a .thumbnails directory (or is, depending on what youre doing), then look at its children and get them. It also gets pictures in the /DCIM directory itself.

File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    File thumbnails = new File(dcim, "/.thumbnails");
    File[] listOfImg = dcim.listFiles();
    if (dcim.isDirectory()){
        //for each child in DCIM directory
        for (int i = 0; i < listOfImg.length; ++i){
            //no thumbnails
            if( !listOfImg[i].getAbsolutePath().equals(thumbnails.getAbsolutePath()) ){
                //only get the directory (100MEDIA, Camera, 100ANDRO, and others)
                if(listOfImg[i].isDirectory()) {
                    //is a parent directory, get children
                    File[] temp = listOfImg[i].listFiles();
                    for(int j = 0; j < temp.length; ++j) {
                        f.add(temp[j].getAbsolutePath());
                    }
                }else if(listOfImg[i].isFile()){
                    //is not a parent directory, get files
                    f.add(listOfImg[i].getAbsolutePath());
                }
            }
        }
    }

I have been looking for a solution to this problem myself. Since different manufacturers have differnet naming conventions for camera folders, this seems to cover all ground best. It works for me pretty well.