Real path of media file using Xamarin.Media => GetMediaFileExtraAsync

Hi there,

I would like to have path to my file in Android that I open using Xamarin.Media.

var picker = new MediaPicker(Activity); Intent intent = picker.GetPickPhotoUI(); StartActivityForResult(intent, 1);

... data.GetMediaFileExtraAsync(Activity).ContinueWith(dataResult => { var path = dataResult.Result.Path;

path is not real path, but temp path like
/emulated/.../.../image_date.jpg

and I would like to see more like
/sd/.../.../realimage.name.jpg

Posts

  • MarkSmith.8123MarkSmith.8123 USXamarin Team, University, XamUProfessors Xamurai
    edited February 2015

    Hi Jakub,

    I'm not sure what you are asking for. The path is a real file path. For example, if I retrieve an existing image from a real device, I'll get back something like:

    /storage/emulated/0/Android/data/XXX.XXX/files/Pictures/temp/IMG_20150202_101655.jpg

    The /storage/emulated/ path is the external storage directory on my Android device (I get the same path if I call Environment.getExternalStorageDirectory).

    However, this is not the actual camera file, which is what I think you are asking about. That path should be something like:

    /storage/emulated/0/DCIM/camera/xxxx

    or possibly /sd/xxx/ if it's on an SD card.

    This is because the picker API returns a temp copy of the file - this is the Android picker, not the component. Normally, you'd not actually use the file at all, but instead use the returned stream and load it into a Bitmap, for example (this is Java):

    final Uri imageUri = imageReturnedIntent.getData();
    final InputStream imageStream = getContentResolver().openInputStream(imageUri);
    final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
    imageView.setImageBitmap(selectedImage);
    

    The component pulls the extra metadata out of the content resolver to get the underlying temp filename. That's what you are getting here. If you'd like to keep the actual file, you will need to copy it off somewhere that you have access to as the file is deleted when the MediaFile is disposed or collected.

    I'm not aware of any way to get to the actual camera file - I don't think the Intent.ACTION_PICK supports it.

    I hope that helps, feel free to add more details if I misunderstood what you are looking for.

    Mark

  • jakub.mielczarkowskijakub.mielczarkowski USUniversity
    edited February 2015

    Well I was using something like:

    Intent myIntent = new Intent ();
    myIntent.SetType ("image/*");
    myIntent.SetAction (Intent.ActionPick);
    this.StartActivityForResult (myIntent, 1);
    

    and then OnActivityResult

    Uri contentUri = data.Data;
    string realPath = Utils.getRealPathFromURI(contentUri, Activity);
    

    Utils.getRealPathFromURI() return me path to real file like you said actual sd card file (not copy)

    So API returns a temp copy, and there is no way to get picked file path? I am using it to check if user is not picking same image :)

  • Another question is why returned Intent data in OnActivityResult is null?

    var picker = new MediaPicker(Activity);
    if (!picker.PhotosSupported) return;
    Intent intent = picker.GetPickPhotoUI();
    StartActivityForResult(intent, 1);
    
    public override void OnActivityResult (int requestCode, int resultCode, Intent data)
    ...
    if (data != null)
    Uri image = data.Data; // image is always null, because data.Data is null :neutral: 
    
  • MarkSmith.8123MarkSmith.8123 USXamarin Team, University, XamUProfessors Xamurai

    Hmm. That sounds kinda like a bug ;)

    Why don't you download the source from https://github.com/xamarin/Xamarin.Mobile and step through it to see if the data is supplied by the underlying Android picker.. let me know!

    Mark

  • Ok I will let you know if I find something

  • MarkSmith.8123MarkSmith.8123 USXamarin Team, University, XamUProfessors Xamurai

    Any luck?

Sign In or Register to comment.