简体   繁体   English

将图像从Android应用程序上传到Google驱动器

[英]Upload image from android application to google drive

I am using the following code to upload the image but it will not work.In the following code the try block shows the error and the file value will be empty any one can help me to solve the problem. 我正在使用以下代码上传图像,但无法正常工作。在以下代码中,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();
      }
    });
  };
}

or is there any way to send files from android application to google drive. 或者有什么方法可以将文件从Android应用程序发送到Google驱动器。

Ok, there is a error in your code: 好的,您的代码中有一个错误:

startActivityForResult(cameraIntent,TAKE_PICTURE);

Just replace that for: 只需将其替换为:

startActivityForResult(cameraIntent,CAPTURE_IMAGE);

Greetings 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Android上传Google云端硬盘图片 - Google Drive image upload from android 从我的Android应用程序将新文档上传到Google驱动器 - Upload a new document to Google drive from my android application 如何从Android应用程序上传db文件到谷歌驱动器? - How to upload a db file to google drive from android application? 我想将图像上传到Google驱动器并在android-eclipse中从Google驱动器检索图像URL吗? - i want to upload image into Google drive and retrieve the image URL from Google drive in android- eclipse ? 如何使用 Google Drive API 从 android 将加密的图像文件上传到 Google Drive - How to upload encrypted image file on google drive from android using Google Drive API 在Android中以编程方式使用Google Drive API将图像上传到Google Drive - Upload image to a google drive using google drive api programatically in android Android无需压缩即可将图像上传到Google驱动器 - Android upload image to google drive without compression 将文件从Android应用上传到Google驱动器 - Upload file from android app to google drive 如何将图像从我的Android应用程序上传到Google驱动器上的特定文件夹 - how to upload an image from my android app to a specific folder on google drive 如何在Android应用程序中将pdf文件上传到Google驱动器 - How to upload a pdf file to google drive in android application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM