简体   繁体   中英

Xamarin media plugin get android image path

I am using the media plugin for xamarin forms (by james montemagno) and the actual taking of the picture and storing it works fine, I have debugged the creation of the image on the emulator and it is stored in

/storage/emulated/0/Android/data/{APPNAME}.Android/files/Pictures/{DIRECTORYNAME}/{IMAGENAME}

however in my app it will get a list of file names from an API I want to check if the image exists in that folder.

The following works fine on IOS

var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, App.IMAGE_FOLDER_NAME);
jpgFilename = System.IO.Path.Combine(jpgFilename, name);

I have tried the 2 following methods for getting it on android but both are incorrect

var documentsDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, App.IMAGE_FOLDER_NAME);
jpgFilename = System.IO.Path.Combine(jpgFilename, name);
Java.IO.File dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DataDirectory + "/" + App.IMAGE_FOLDER_NAME + "/" + name);

dir ends up as /storage/emulated/0/data/{DIRECTORY}/{IMAGENAME}

jpgFileName ends up as /data/data/{APPNAME}.Android/files/{DIRECTORYNAME}/{IMAGENAME}

I dont want to hardcode anything in the paths but neither of these are right. I could not find anything in the GIT documentation for getting the file path except by looking at the path of the file created when taking a picture

The problem

I had the same kind of issue with Xamarin Media Plugin. For me, the problem is:

  • we don't really know where the plugin save the picture on android.

After reading all documentation I found, I just noted this:

... When your user takes a photo it will still store temporary data , but also if needed make a copy to the public gallery (based on platform). In the MediaFile you will now see a AlbumPath that you can query as well.

( from: Github plugin page )

  • so you can save your photo to your phone gallery (it will be public) but the path is known.
  • and we don't know what means "store the temporary data".

Solution

After investigating on how/where an app can store data, I found where the plugin stores photos on Android >> so I can generate back the full file names

In your Android app, the base path you are looking for is:

var basePath = Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath

It references your app's external private folder . It looks like that:

/storage/emulated/0/Android/data/com.mycompany.myapp/files


So finally to get your full file's path:

var fullPath = basePath + {DIRECTORYNAME} + {FILENAME};


I suggest you to make a dependency service , for instance 'ILocalFileService', that will expose this 'base path' property.

Please let me know if it works for you !

I resolved a similar problem. I wanted to collect all files for my app in a folder visible to all users.

var documentsDirectory = Android.OS.Environment.GetExternalStoragePublicDirectory(
                                                Android.OS.Environment.DirectoryDocuments);

Android下的文件管理器

If you want to create Directory , add to your class using System.IO; and you have the same functions in a normal .NET application.

if (!Directory.Exists(path))
    Directory.CreateDirectory(path);

If you want to create files or directory, you can use PCLStorage .

public async Task<bool> CreateFileAsync(string name, string context)
{
    // get hold of the file system
    IFolder folder = FileSystem.Current.LocalStorage;

    // create a file, overwriting any existing file
    IFile file = await folder.CreateFileAsync(name, 
                                              CreationCollisionOption.ReplaceExisting);

    // populate the file with some text
    await file.WriteAllTextAsync(context);

    return true;
}

to get the private path var privatePath = file.Path;

to get the public Album path var publicAlbumPath = file.AlbumPath;

se the documentation here https://github.com/jamesmontemagno/MediaPlugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM