[英]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我正在使用 xamarin 表单的媒体插件(由 james montemagno 提供)并且实际拍摄照片并存储它工作正常,我已经调试了模拟器上图像的创建并将其存储在
/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.但是在我的应用程序中,它将从 API 获取文件名列表,我想检查该文件夹中是否存在图像。
The following works fine on IOS以下在 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我已经尝试了以下 2 种在 android 上获取它的方法,但两者都不正确
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} dir 最终为 /storage/emulated/0/data/{DIRECTORY}/{IMAGENAME}
jpgFileName ends up as /data/data/{APPNAME}.Android/files/{DIRECTORYNAME}/{IMAGENAME} jpgFileName 最终为 /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
除了查看拍照时创建的文件的路径外,我在 GIT 文档中找不到任何用于获取文件路径的内容
I had the same kind of issue with Xamarin Media Plugin.我在使用 Xamarin Media Plugin 时遇到了同样的问题。 For me, the problem is:
对我来说,问题是:
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.
在 MediaFile 中,您现在将看到您也可以查询的 AlbumPath。
( from: Github plugin page ) (来自: Github 插件页面)
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在调查了应用程序如何/在哪里存储数据之后,我发现了该插件在 Android 上存储照片的位置 >>这样我就可以生成完整的文件名
In your Android app, the base path you are looking for is:在您的 Android 应用程序中,您要查找的基本路径是:
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.我建议您创建一个依赖服务,例如“ILocalFileService”,它将公开此“基本路径”属性。
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);
If you want to create Directory
, add to your class using System.IO;
如果要创建
Directory
,请using System.IO;
添加到您的类using System.IO;
and you have the same functions in a normal .NET application.并且您在普通 .NET 应用程序中具有相同的功能。
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
If you want to create files or directory, you can use PCLStorage
.如果要创建文件或目录,可以使用
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;获取私有路径 var privatePath = file.Path;
to get the public Album path var publicAlbumPath = file.AlbumPath;获取公共相册路径 var publicAlbumPath = file.AlbumPath;
se the documentation here https://github.com/jamesmontemagno/MediaPlugin在此处查看文档https://github.com/jamesmontemagno/MediaPlugin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.