2017-09-15 9 views
-1

こんにちは、私はアンドロイドアプリからGoogleドライブに画像をアップロードします。イメージをアップロードする必要があります_FileをAndroidアプリからGoogleドライブにダウンロードするC#

アプリケーションは、写真を撮ると、それを格納します。

public static File _file; 

私はGoogleに_fileアップロードしたい私は接続して、私はこのコードで空のJPGファイル作成

ドライブ:

`using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using MySql.Data.MySqlClient; 
using System.Data; 
using Google.Apis.Auth.OAuth2; 
using Google.Apis.Drive.v3.Data; 
using Google.Apis.Services; 
using Google.Apis.Util.Store; 
using Google.Apis.Upload; 
using Google.Apis.Http; 
using Android.Util; 
using Android.Gms.Common; 
using Android.Gms.Common.Apis; 
using Android.Gms.Drive; 
using System.Threading.Tasks; 
using Java.IO; 
using Google.Apis.Drive.v3; 

namespace Fotonet 
{ 
[Activity(Label = "Activity2")] 
public class Activity2 : Activity, GoogleApiClient.IConnectionCallbacks, IResultCallback, IDriveApiDriveContentsResult 
{ 
const string TAG = "Fotonet"; 
const int REQUEST_CODE_RESOLUTION = 3; 

GoogleApiClient _googleApiClient; 

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 

    SetContentView(Resource.Layout.prova2); 

    Button button = FindViewById<Button>(Resource.Id.myButton); 
    button.Click += delegate 
    { 
     Activity1.Global.count = 2; 

     if (_googleApiClient == null) 
     { 
      _googleApiClient = new GoogleApiClient.Builder(this) 
       .AddApi(DriveClass.API) 
       .AddScope(DriveClass.ScopeFile) 
       .AddConnectionCallbacks(this) 
       .AddOnConnectionFailedListener(OnConnectionFailed) 
       .Build(); 
     } 
     if (!_googleApiClient.IsConnected) 
      _googleApiClient.Connect(); 



    }; 



} 

protected void OnConnectionFailed(ConnectionResult result) 
{ 
    Log.Info(TAG, "GoogleApiClient connection failed: " + result); 
    if (!result.HasResolution) 
    { 
     GoogleApiAvailability.Instance.GetErrorDialog(this, result.ErrorCode, 0).Show(); 
     return; 
    } 
    try 
    { 
     result.StartResolutionForResult(this, REQUEST_CODE_RESOLUTION); 
    } 
    catch (IntentSender.SendIntentException e) 
    { 
     Toast.MakeText(this, e.ToString(), ToastLength.Short).Show(); 
    } 
} 

public void OnConnected(Bundle connectionHint) 
{ 
    Toast.MakeText(this, "Connesso a Google", ToastLength.Short).Show(); 
    DriveClass.DriveApi.NewDriveContents(_googleApiClient).SetResultCallback(this); 
    Activity1.Global.count += 1; 
    //StartActivity(typeof(MainActivity)); 
} 

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) 
{ 
    base.OnActivityResult(requestCode, resultCode, data); 
    if (requestCode == REQUEST_CODE_RESOLUTION) 
    { 
     switch (resultCode) 
     { 
      case Result.Ok: 
       _googleApiClient.Connect(); 
       break; 
      case Result.Canceled: 
       Toast.MakeText(this, "Unable to sign in", ToastLength.Short).Show(); 
       Log.Error(TAG, "Unable to sign in, is app registered for Drive access in Google Dev Console?"); 
       break; 
      case Result.FirstUser: 
       Toast.MakeText(this, "Unable to sign in: RESULT_FIRST_USER", ToastLength.Short).Show(); 
       Log.Error(TAG, "Unable to sign in: RESULT_FIRST_USER"); 
       break; 
      default: 
       Log.Error(TAG, "Should never be here: " + resultCode); 
       return; 
     } 
    } 
} 

void IResultCallback.OnResult(Java.Lang.Object result) 
{ 
    var contentResults = (result).JavaCast<IDriveApiDriveContentsResult>(); 
    if (!contentResults.Status.IsSuccess) // handle the error 
     return; 
    Task.Run(() => 
    { 
     var image = new FileOutputStream(App._file); 

     //writer.Write("Stack Overflow"); 
     //writer.Close(); 
     MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
       .SetTitle("Fotonet.jpg") 
       .SetMimeType("image/jpeg") 
       .Build(); 
     DriveClass.DriveApi 
        .GetRootFolder(_googleApiClient) 
        .CreateFile(_googleApiClient, changeSet, contentResults.DriveContents); 
    }); 
} 

public void OnConnectionSuspended(int cause) 
{ 
    throw new NotImplementedException(); 
} 

public IDriveContents DriveContents 
{ 
    get 
    { 
     throw new NotImplementedException(); 
    } 
} 

public Statuses Status 
{ 
    get 
    { 
     throw new NotImplementedException(); 
    } 
} 
} 
} ` 

この空のファイルを埋める方法は?

ありがとうございました!

アップロードしようとすると、空のファイルjpgが作成されます。

私の悪い英語のために申し訳ありません。

+1

質問のタイトルでは、Googleドライブにファイルをアップロードする方法を尋ねる。しかし問題のボディでは、スクリーンショットをファイルに保存する方法を尋ねます。あなたの本当の質問は何ですか? –

+0

App._Fileをアップロードします。私はこれに考える:空のjpegファイルを作成し、それを書く。どのように解決するか考えていますか?あなたは最善の方法を知っていますか? –

答えて

0

この空のファイルの書き方は?

あなただけの基本的な画像ファイルを作成したい場合は、次のメソッドを使用します。

private Bitmap CreateBitmap(int w, int h) 
{ 
    Bitmap bitmap = Bitmap.CreateBitmap(w, h, Config.Argb8888); 
    int[] pixels = new int[w * h]; 
    for (int x = 0; x < w * h; x++) 
    { 
     pixels[x] = Color.Blue.ToArgb(); 
    } 
    bitmap.SetPixels(pixels, 0, w, 0, 0, w, h); 
    return bitmap; 
} 

をそして、あなたはGoogleドライブにイメージファイルをアップロードするには、次のコードを使用することができます。

DriveClass.DriveApi.NewDriveContents(client).SetResultCallback(new System.Action<IDriveApiDriveContentsResult>((result)=> { 

      //Create MetadataChangeSet 
      MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() 
       .SetTitle("Test.jpg") 
       .SetMimeType("image/jpeg").Build(); 

      var contents = result.DriveContents; 
      //create the bitmap 
      Bitmap bitmap = CreateBitmap(800, 800); 
      //push the bitmap data to DriveContents 
      bitmap.Compress(CompressFormat.Jpeg, 1, contents.OutputStream); 
      IntentSender intentSender = DriveClass.DriveApi 
        .NewCreateFileActivityBuilder() 
        .SetInitialMetadata(metadataChangeSet) 
        .SetInitialDriveContents(contents) 
        .Build(client); 

      StartIntentSenderForResult(intentSender, 2, null, 0, 0, 0); 
     })); 
関連する問題