Android device specific camera path issue

百般思念 提交于 2019-11-28 09:11:05

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).

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();
    }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!