2017-07-04 40 views
0

画像をピッキングするためのコードを書いています&画像をアップロードして特定の画像をエミュレータに保存していますが、Xamarinを使用してAndroidファイルシステムのファイルにアクセスするにはどうすればよいですか?アップロード方法Xamarinの内蔵ストレージまたは外部ストレージからファイルをアップロードするには?

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(); 
      } 
     } 
    } 
} 

答えて

0

followingを見てください:

string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
string filename = Path.Combine(path, "myfile.txt"); 

using (var streamWriter = new StreamWriter(filename, true)) 
{ 
    streamWriter.WriteLine(DateTime.UtcNow); 
} 

using (var streamReader = new StreamReader(filename)) 
{ 
    string content = streamReader.ReadToEnd(); 
    System.Diagnostics.Debug.WriteLine(content); 
} 
0
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. 
     } 
    } 
をOnActivityResult呼び出します
関連する問題