SImple way to get stock camera images location in Android /DCIM/?

南笙酒味 提交于 2019-12-12 01:13:38

问题


I have an idea for an app, and to do it i need to be in the folder where the stock camera stores it's pictures. But since most manufactureres name the folder inside DCIM diferently, is there a way to find the specific folder that the camera saves pictures into. Also I can't list and open the first result because, for example i have 5 folders in there. Thanks!


回答1:


One of the solution is to insert photo to MediaStore using ContentResolver (it will create empty JPG file), retrieve its path and delete it from MediaStore (file will be deleted as well).

public static File getPhotoDirPath(ContentResolver cr)
{
    try
    {
        Uri takenPhotoUri=cr.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues( 1 ) );
        if ( takenPhotoUri == null )
            return null;
        String photoFilePath=null;
        Cursor cursor = cr.query( takenPhotoUri, new String[] { MediaColumns.DATA }, null, null, null );
        if ( cursor != null )
        {
            int dataIdx = cursor.getColumnIndex( MediaColumns.DATA );
            if (dataIdx>=0&&cursor.moveToFirst())
                photoFilePath = cursor.getString( dataIdx );
            cursor.close();
        }
        cr.delete( takenPhotoUri, null, null );
        if (photoFilePath!=null)
            return new File(photoFilePath).getParentFile();
        return null;
    }
    catch (Exception ex)
    {//insert or delete failed
        return null;
    }
}   

Note that in some cases (the same as when camera apps are not able to save photos) photos may not be inserted successfully eg. SD card is removed (if device cannot emulate external storage), external storage is mounted read-only or some directory in path is write protected etc.




回答2:


you may take Environment.DIRECTORY_PICTURES as the valid directory.

The following will refer to that directory (but it is not guaranteed in all devices):

File storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES);



回答3:


So i ended up doing it with .exists()

String abcd = "is it working ?";
          File pathimg = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
          File testimg = new File (pathimg, "100MEDIA/");
          if (testimg.exists()){abcd = testimg.toString();}
          else {
              File testimg2 = new File (pathimg, "100ANDRO/");
              if (testimg2.exists()){abcd = testimg2.toString();}
              else {
                  File testimg3 = new File (pathimg, "Camera/");
                  if (testimg3.exists()){abcd = testimg3.toString();}
                  else {
                      File testimg4 = new File (pathimg, "100LGDSC/");
                      if (testimg4.exists()){abcd = testimg4.toString();}
                      else {
                          abcd = "It's not working";
                      }
                  }                   
              }
          }

On my G2 with the folder "100LGDSC" it's working.



来源:https://stackoverflow.com/questions/21743634/simple-way-to-get-stock-camera-images-location-in-android-dcim

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