我從來沒有發現跨媒體插件上傳現有的照片代碼工作。相反,我使用依賴注入來實現本地圖庫選擇。一旦你有了url,你可以將它貼在圖像視圖中,或者將它作爲位圖或其他任何東西在本地代碼中膨脹。實際上,我將Model.Photo傳回照片的名稱和字節[],但這取決於您。
PCL
var x = await DependencyService.Get<IMedia>().UploadGallery();
的Android
class Media_Android : Activity, IMedia
{
public async Task<string> UploadGallery()
{
var activity = Forms.Context as MainActivity;
Intent intent = new Intent();
intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
var result = await activity.StartActivityForResultAsync(intent);
if(result.Data != null)
{
Android.Net.Uri myUri = result.Data.Data;
var getString = this.GetPathToImage(myUri);
if (getString != null)
{
return getString;
}
else
{
return null;
}
}
else
{
return null;
}
}
}
private string GetPathToImage(Android.Net.Uri uri)
{
int currentapiVersion = (int)Android.OS.Build.VERSION.SdkInt; // Gets the Android version number for ex:- Lollipop 21-22, KitKat 19–20, JellyBean: 16-18.
string path = "";
// "KitKat 4.4(KitKat) and above compatible"
if (currentapiVersion > (int)Android.OS.BuildVersionCodes.JellyBeanMr2)
{
Android.Database.ICursor cursor1 = Forms.Context.ContentResolver.Query(uri, null, null, null, null);
cursor1.MoveToFirst();
string document_id = cursor1.GetString(0);
if(document_id.Contains(":"))
{
document_id = document_id.Split(':')[1];
}
cursor1.Close(); //Extract the ID from the URI
cursor1 = Forms.Context.ContentResolver.Query(
Android.Provider.MediaStore.Images.Media.ExternalContentUri,
null, MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new System.String[] { document_id }, null);
cursor1.MoveToFirst();
path = cursor1.GetString(cursor1.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
cursor1.Close(); //Query the device database, to get the Image based on the ID
}
//"JellyBean 4.1 - 4.3.1 (JellyBean) and below compatible"
if (currentapiVersion <= (int)Android.OS.BuildVersionCodes.JellyBeanMr2)
{
System.String[] projection = new System.String[] { Android.Provider.MediaStore.MediaColumns.Data };
contentResolver = Forms.Context.ContentResolver;
Android.Database.ICursor cursor = contentResolver.Query(uri, projection, null, null, null);
if (cursor != null && cursor.Count > 0)
{
cursor.MoveToFirst();
int index = cursor.GetColumnIndex(Android.Provider.MediaStore.MediaColumns.Data);
path = cursor.GetString(index);
}
}
return path;
}
的iOS
這是很容易比Android只是谷歌。
如果您的應用程序崩潰,請添加堆棧跟蹤和其他相關信息。 – Cheesebaron
添加調試器並檢查應用程序崩潰的行。 –
你的代碼適用於我與Plugin.Media,版本= 2.6.2.0我假設LocalPathLable是一個標籤,FileImage是圖像和_mediaFile被定義爲類的成員 –