RRTW
RRTW

Reputation: 3190

android get photo path without take photo

I would like to get camera photo real path, without taking photo.

What I'm using for now is:

String path=""; //The real path I want...
String[] projection=new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_TAKEN};     
final Cursor c=context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN+" DESC"); 
if(c!=null)
{
  c.moveToFirst();
  path=c.getString(1);
  Log.i(Constants.TAG, "File Path: "+path);
  path=path.substring(0, path.lastIndexOf('/')+1);
}

But the result from above just changed sometime...

For normal case, it will be /storage/ext_sd/DCIM/100MEDIA/

(I'm using HTC Butterfly, android 4.1.1, exactly same module called DroidDNA in United Stats)

This afternoon, after I move some of my photo to another folder (operate on phone, not PC)

The result changed to /storage/ext_sd/[some_other_of_my_image_folder]/

How can I get camera photo's real path ? Please help me :-(

Edit: I'm trying another way like following:

Log.i(Constants.TAG, "Photo path: "+f.getPath());
if(f.exists())
{
  File test1=new File(f, "100MEDIA/");
  if (test1.exists())
  {f=test1;}
  else
  {
    File test2=new File(f, "100ANDRO/");
    if(test2.exists())
    {f=test2;}
    else
    {
      File test3=new File(f, "Camera/");
      if(!test3.exists())
      {test3.mkdirs();}
      f=test3;
    }
  }
}
else
{
  f=new File(f, "Camera/");
  f.mkdirs();
}

It returned path like: /storage/sdcard0/DCIM/100MEDIA/, which is my phone's internal 16GB space.

I had already set my camera photo store to external microSD, which path should be /ext_sd/DCIM/100MEDIA/

Really need help, MEDIC !

Upvotes: 4

Views: 1976

Answers (1)

RRTW
RRTW

Reputation: 3190

2 people vote up my question, so I guess maybe other programmers are facing same issue.

I got a way to overcome this, and following is my code :

String path="";
String[] projection=new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_TAKEN};     
final Cursor c=context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns.DATE_TAKEN+" DESC");
boolean isOK=false; //Found a DCIM path ?
if(c!=null)
{
  c.moveToFirst();
  while(!isOK)
  {
    path=c.getString(1);
    Log.i(Constants.TAG, "File Path: "+path);
    path=path.substring(0, path.lastIndexOf('/')+1);
    isOK=!(path.indexOf("DCIM")==-1); //Is thie photo from DCIM folder ?

    c.moveToNext(); //Add this so we don't get an infinite loop if the first image from
                    //the cursor is not from DCIM
  }
}
Log.i(Constants.TAG, "Only Path: "+path);

Express :

First, that's my SD card

  • /DCIM/100MEDIA/xxx.JPG (photo from camera)
  • /Media/image/comics/xman/01.JPG
  • ...
  • ..
  • .
  • /Media/image/hot/youknowwhatitis/hot01.JPG

I always read first record from context.getContentResolver().query's cursor.

Normally, it return photos inside DCIM first, and then other photo/image/picture.

One day morning, I move a file from DCIM to another folder, (via android's Gallery APP)

and context.getContentResolver().query's first record changed to other non DCIM content,

bring this issue to me...orz


THIS IS NOT PERFECT solution, if user didn't take any photos to DCIM, this solution will become useless.

Since I'm developing a multimedia APP, guess user should/must a photo lover... That's the best way I can do until now, hope it bring little help to others.

Upvotes: 3

Related Questions