簡體   English   中英

將圖像從Android應用程序上傳到Google驅動器

[英]Upload image from android application to google drive

我正在使用以下代碼上傳圖像,但無法正常工作。在以下代碼中,try塊顯示錯誤,文件值將為空,任何人都可以幫助我解決問題。

public class MainActivity extends Activity {

static final int REQUEST_ACCOUNT_PICKER = 1;
  static final int REQUEST_AUTHORIZATION = 2;
  static final int CAPTURE_IMAGE = 3;
private static final int TAKE_PICTURE = 2;
ImageView im;

  private static Uri fileUri;
  java.io.File fileContent;
  FileContent mediaContent; 
  java.io.File f;
  File body;
  File nfile;
  File file;
  private static Drive service;
  private GoogleAccountCredential credential;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    im=(ImageView)findViewById(R.id.imageView1);
    credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
    startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);

  }

  @Override
  protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
    case REQUEST_ACCOUNT_PICKER:
      if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
        String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        if (accountName != null) {
          credential.setSelectedAccountName(accountName);
          service = getDriveService(credential);
          startCameraIntent();

        }
      }
      break;
    case REQUEST_AUTHORIZATION:
        Toast.makeText(getApplicationContext(), ""+REQUEST_AUTHORIZATION,100).show();
      if (resultCode == Activity.RESULT_OK) {
          saveFileToDrive();
      } else {
        startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);

      }
      break;
    case CAPTURE_IMAGE:
      if (resultCode == Activity.RESULT_OK) {
        saveFileToDrive();
      }
    }
  }

  private void startCameraIntent() {
    String mediaStorageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES).getPath();



    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date(0, 0, 0));
    f=new java.io.File(Environment.getExternalStorageDirectory(),"test"+timeStamp+".jpg");


    fileUri=Uri.fromFile(f);

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(cameraIntent,TAKE_PICTURE);


  }

  private void saveFileToDrive() {


      Toast.makeText(getApplicationContext(), "start",100).show();
    Thread t = new Thread(new Runnable() {
      @Override
      public void run() {
          try
          {
        // File's binary content
            fileContent = new java.io.File(fileUri.getPath());
            mediaContent= new FileContent("image/jpeg", fileContent);


            body=new File();

            body.setTitle(fileContent.getName());
            body.setMimeType("image/jpeg");
            //file=body;

           file= service.files().insert(body, mediaContent).execute();


                //file=service.files().insert(body).execute();
                showToast("try run: ");


          if (file!=null)
          {
            showToast("Photo uploaded: " + file.getTitle());

            startCameraIntent();
          }
          else
          {     
                showToast("error");
          }
      }

          catch (UserRecoverableAuthIOException e) {
              startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                showToast("first catch");

            } catch (IOException e) {
              e.printStackTrace();
                showToast("second catch"+file);

            }
          finally
          {

                showToast("finally");


          }
          }

    });
    t.start();


      }



  private Drive getDriveService(GoogleAccountCredential credential) {
    return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
        .build();
  }

  public void showToast(final String toast) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Toast.makeText(getApplicationContext(),toast,100).show();
      }
    });
  };
}

或者有什么方法可以將文件從Android應用程序發送到Google驅動器。

好的,您的代碼中有一個錯誤:

startActivityForResult(cameraIntent,TAKE_PICTURE);

只需將其替換為:

startActivityForResult(cameraIntent,CAPTURE_IMAGE);

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM