C# 如何在Xamarin中从内部或外部存储上载任何文件?
我已经编写了一些代码,用于拾取图像和上传图像,并在emulator中获取特定的图像,但我如何使用Xamarin从Android文件系统访问任何文件,那么我应该使用什么来实现呢C# 如何在Xamarin中从内部或外部存储上载任何文件?,c#,xamarin,xamarin.android,C#,Xamarin,Xamarin.android,我已经编写了一些代码,用于拾取图像和上传图像,并在emulator中获取特定的图像,但我如何使用Xamarin从Android文件系统访问任何文件,那么我应该使用什么来实现呢 namespace XAFileUpload_2._0 { [Activity(Label = "XAFileUpload 2.0", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity
namespace XAFileUpload_2._0
{
[Activity(Label = "XAFileUpload 2.0", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
//Pick Id to know the pick item no.
public static readonly int PickImageId = 1000;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.Browse);
button.Click += BrowseButtonOnClick;
}
void BrowseButtonOnClick(object sender, EventArgs eventArs)
{
Intent = new Intent();
Intent.SetType("image/*");
Intent data = Intent.SetAction(Intent.ActionGetContent);
try
{
StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);
}
catch (ActivityNotFoundException ex)
{
Toast.MakeText(this, "Please install a File Manager.", ToastLength.Long).Show();
}
catch (Exception ex)
{
Toast.MakeText(this, "Error occured ", ToastLength.Long).Show();
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
UploadFile(data);
}
}
void UploadFile(Intent data)
{
ICursor cursor = null;
try
{
Button upButton = FindViewById<Button>(Resource.Id.Upload);
// assuming image
var docID = DocumentsContract.GetDocumentId(data.Data);
var id = docID.Split(':')[1];
var whereSelect = MediaStore.Images.ImageColumns.Id + "=?";
var projections = new string[] { MediaStore.Images.ImageColumns.Data };
// Try internal storage first
cursor = ContentResolver.Query(MediaStore.Images.Media.InternalContentUri, projections, whereSelect, new string[] { id }, null);
if (cursor.Count == 0)
{
// not found on internal storage, try external storage
cursor = ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, projections, whereSelect, new string[] { id }, null);
}
var colData = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.Data);
cursor.MoveToFirst();
var fullPathToImage = cursor.GetString(colData);
string fpti = cursor.GetString(colData);
string address = "http://192.168.3.157:82/Values/DownFile";
upButton.Click += (sender, args) =>
{
try
{
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.SetTitle("Please wait...");
progressDialog.SetCancelable(false);
Task.Run(() =>
{
RunOnUiThread(() =>
{
progressDialog.Show();
});
using (WebClient client = new WebClient())
{
client.UploadFile(address, fullPathToImage);
}
RunOnUiThread(() =>
{
progressDialog.Dismiss();
string msg = "Upload Completed";
Toast.MakeText(this, msg, ToastLength.Long).Show();
});
});
}
catch (Exception ex)
{
Log.Error("MediaPath", ex.Message);
}
};
}
catch (Exception ex)
{
Log.Error("MediaPath", ex.Message);
}
finally
{
cursor?.Close();
cursor?.Dispose();
}
}
}
}
namespace XAFileUpload\u 2.\u 0
{
[活动(Label=“XAFileUpload 2.0”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
//选择Id以了解选择项目编号。
公共静态只读int-PickImageId=1000;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
Button Button=FindViewById(Resource.Id.Browse);
按钮。单击+=浏览按钮取消单击;
}
void BrowseButtonOnClick(对象发送者、事件参数、事件)
{
Intent=新Intent();
Intent.SetType(“image/*”);
Intent数据=Intent.SetAction(Intent.ActionGetContent);
尝试
{
StartActivityForResult(Intent.CreateChooser(Intent,“选择图片”),PickImageId);
}
捕获(ActivityNotFoundException ex)
{
Toast.MakeText(这是“请安装文件管理器”,ToastLength.Long).Show();
}
捕获(例外情况除外)
{
Toast.MakeText(此“出错”,ToastLength.Long).Show();
}
}
受保护的覆盖void OnActivityResult(int请求代码、结果代码、意图数据)
{
if((requestCode==PickImageId)&&&(resultCode==Result.Ok)&&(data!=null))
{
上传文件(数据);
}
}
无效上载文件(意图数据)
{
ICursor cursor=null;
尝试
{
按钮upButton=FindViewById(Resource.Id.Upload);
//假象
var docID=DocumentsContract.GetDocumentId(data.data);
var id=docID.Split(“:”)[1];
var whereSelect=MediaStore.Images.ImageColumns.Id+“=?”;
var projections=新字符串[]{MediaStore.Images.ImageColumns.Data};
//首先尝试内部存储
cursor=ContentResolver.Query(MediaStore.Images.Media.InternalContentUri,projections,whereSelect,新字符串[]{id},null);
如果(cursor.Count==0)
{
//在内部存储上找不到,请尝试外部存储
cursor=ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri,projections,whereSelect,新字符串[]{id},null);
}
var colData=cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.Data);
cursor.MoveToFirst();
var fullPathToImage=cursor.GetString(colData);
字符串fpti=cursor.GetString(colData);
字符串地址=”http://192.168.3.157:82/Values/DownFile";
向上按钮。单击+=(发件人,参数)=>
{
尝试
{
ProgressDialog ProgressDialog=新建ProgressDialog(此);
progressDialog.SetTitle(“请稍候…”);
progressDialog.SetCancelable(假);
Task.Run(()=>
{
RunOnUiThread(()=>
{
progressDialog.Show();
});
使用(WebClient=newWebClient())
{
上载文件(地址,fullPathToImage);
}
RunOnUiThread(()=>
{
progressDialog.disclose();
string msg=“上传完成”;
Toast.MakeText(this,msg,ToastLength.Long).Show();
});
});
}
捕获(例外情况除外)
{
Log.Error(“MediaPath”,例如Message);
}
};
}
捕获(例外情况除外)
{
Log.Error(“MediaPath”,例如Message);
}
最后
{
光标?.Close();
游标?.Dispose();
}
}
}
}
看一下:
它打开“图像拾取”对话框,选择图像后自动调用ActivityResult方法
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
//Your code to upload image.
//image uri is in data object.
}
}
Intent = new Intent();
Intent.SetType("image/*");
Intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
//Your code to upload image.
//image uri is in data object.
}
}