Domanda

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!

È stato utile?

Soluzione 2

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.

Altri suggerimenti

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.

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top