android中的google drive应用程序将照片上传到硬盘。下面列出了代码

android中的google drive应用程序将照片上传到硬盘。下面列出了代码,android,android-activity,google-drive-api,Android,Android Activity,Google Drive Api,我在3个不同的类中编写代码 第一个用于连接 第二个用于从画廊拍摄图像 第三个用于将图像上载到驱动器 在第一个类中,我将GoogleAppClient对象设置为静态对象,如 static GoogleApiClient getmGoogleapiclient() { return mGoogleApiClient; } without null value i got mGoogleApiClient 在第3个类中,执行此行时: Drive.Dri

我在3个不同的类中编写代码

  • 第一个用于连接
  • 第二个用于从画廊拍摄图像
  • 第三个用于将图像上载到驱动器
  • 在第一个类中,我将
    GoogleAppClient
    对象设置为静态对象,如

     static GoogleApiClient getmGoogleapiclient()
         {
            return mGoogleApiClient;
           }  without null value i got  mGoogleApiClient 
    
    在第3个类中,执行此行时:

    Drive.DriveApi.newDriveContents(Bqdriveactivity.getmGoogleapiclient()).setResultCallback(new ResultCallback<DriveContentsResult>() {
    
    Drive.DriveApi.newDriveContents(Bqdriveactivity.getMgoogleAppClient()).setResultCallback(new ResultCallback()){
    
    出现问题时,pgm内部没有错误,但不会执行

    这是密码

       public class Uploadimage extends Activity 
     {
        private static final String TAG = "Jimmy-android-drive-application";
        private static final int REQUEST_CODE_CREATOR = 0;
    //  private GoogleApiClient mGoogleApiClient ;
    
        public Bitmap mBitmapToSave;
        ArrayList<Bitmap> bpobj=new ArrayList<Bitmap>();
        String images;
        ArrayList<String> stringArray;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_bqdriveactivity);
              stringArray= getIntent().getStringArrayListExtra("imagearray");
    
              if(stringArray!=null){
              Iterator<String> i = stringArray.iterator();
                do 
                  {  
                      images=i.next();
                      Log.e("name is: ",images);
                      mBitmapToSave=BitmapFactory.decodeFile(images);
                     if(mBitmapToSave==null){
                          Log.e("ERROR? : ","mBitmap is null");
                       }
                      else
                       {
                          //Log.e("ERROR","not null");
                       }
                        try {
                            Thread thread = new Thread(new myThread());
                            thread.start();
    
    
                             } catch (Exception e) {
    
                               e.printStackTrace();
                             }
              }while (i.hasNext());
              }
                          }
    
        class myThread implements Runnable {
    
            @Override
            public void run() {
                saveFileToDrive(mBitmapToSave);
    
            }
    
    
        }
    
    public void saveFileToDrive( Bitmap bp)  {
            // Start by creating a new contents, and setting a callback.
            Log.i(TAG, "Creating new contents.");
    
          final  Bitmap image = bp;
            if(bp==null){
                 Log.e(TAG,"bitmap is null");
            }
            else
            {
                Log.e(TAG,"bitmap is not null");
            }
    
               if(Bqdriveactivity.getmGoogleapiclient()==null){
                   Log.e(TAG,"null client"); 
               }
               else
               {
               Log.e(TAG,"not null");
               }
    
    公共类上载映像扩展活动
    {
    私有静态最终字符串TAG=“Jimmy android驱动器应用程序”;
    私有静态最终整数请求\代码\创建者=0;
    //私人GoogleapClient MGoogleapClient;
    公共位图mbitmatosave;
    ArrayList bpobj=新的ArrayList();
    字符串图像;
    ArrayList字符串数组;
    @凌驾
    创建时的公共void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity\u bqdriveactivity);
    stringArray=getIntent().getStringArrayListExtra(“imagearray”);
    if(stringArray!=null){
    迭代器i=stringArray.Iterator();
    做
    {  
    images=i.next();
    Log.e(“名称为:”,图像);
    MbitMatoSave=BitmapFactory.decodeFile(图像);
    if(mbitmatosave==null){
    Log.e(“错误:”,“mBitmap为空”);
    }
    其他的
    {
    //Log.e(“错误”、“非空”);
    }
    试一试{
    线程线程=新线程(new myThread());
    thread.start();
    }捕获(例外e){
    e、 printStackTrace();
    }
    }而(i.hasNext());
    }
    }
    类myThread实现可运行{
    @凌驾
    公开募捐{
    saveFileToDrive(mbitmatoposave);
    }
    }
    public void saveFileToDrive(位图bp){
    //首先创建一个新的内容,并设置一个回调。
    Log.i(标记“创建新内容”);
    最终位图图像=bp;
    如果(bp==null){
    Log.e(标记“位图为空”);
    }
    其他的
    {
    Log.e(标记“位图不为空”);
    }
    if(Bqdriveactivity.getmgoogleapClient()==null){
    Log.e(标记为“空客户端”);
    }
    其他的
    {
    Log.e(标记“非空”);
    }
    
    到这里代码执行

                 Drive.DriveApi.newDriveContents(Bqdriveactivity.getmGoogleapiclient()).setResultCallback(new ResultCallback<DriveContentsResult>() {
    
                 public void onResult(DriveContentsResult result) {
                     Log.i(TAG, "inside drive contents");
                     if (!result.getStatus().isSuccess()) {
                         Log.i(TAG, "Failed to create new contents.");
                         return;
                     }
    
                     Log.i(TAG, "New contents created.");
                     // Get an output stream for the contents.
                     OutputStream outputStream = result.getDriveContents().getOutputStream();
                     Log.i("TAG","getoutputstream");
                     // Write the bitmap data from it.
                     ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                     Log.i("TAG","before compress");
                     image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
                     Log.i("TAG","after compress");
                     try {
                         outputStream.write(bitmapStream.toByteArray());
                          Log.i("TAG","inside try");
                     } catch (IOException e1) {
                         Log.i(TAG, "Unable to write file contents.");
                     }
                     // Create the initial metadata - MIME type and title.
                     // Note that the user will be able to change the title later.
                     MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                             .setMimeType("image/jpeg").setTitle("Android Photo.png").build();
                     // Create an intent for the file chooser, and start it.
                     IntentSender intentSender = Drive.DriveApi
                             .newCreateFileActivityBuilder()
                             .setInitialMetadata(metadataChangeSet)
                             .setInitialDriveContents(result.getDriveContents())
                             .build(Bqdriveactivity.getmGoogleapiclient());
                     try {
                         startIntentSenderForResult(
                                 intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
                     } catch (SendIntentException e) {
                         Log.i(TAG, "Failed to launch file chooser.");
                     }
                 }  
                     });
       }
    
    
        protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
            switch (requestCode) {
    
                case REQUEST_CODE_CREATOR:
                    // Called after a file is saved to Drive.
                    if (resultCode == RESULT_OK) {
                        Log.i(TAG, "Image successfully saved.");
                        mBitmapToSave = null;
    
                    }
                    break;
            }
        }
    
    
    
                     }
    
    Drive.DriveApi.newDriveContents(Bqdriveactivity.getMgoogleAppClient()).setResultCallback(new ResultCallback()){
    public void onResult(DriveContentsResult结果){
    Log.i(标签“驱动器内部内容”);
    如果(!result.getStatus().issucess()){
    Log.i(标记“未能创建新内容”);
    返回;
    }
    Log.i(标记“创建的新内容”);
    //获取内容的输出流。
    OutputStream OutputStream=result.getDriveContents().getOutputStream();
    Log.i(“标记”、“getoutputstream”);
    //从中写入位图数据。
    ByteArrayOutputStream bitmapStream=新建ByteArrayOutputStream();
    Log.i(“标签”、“压缩前”);
    image.compress(Bitmap.CompressFormat.PNG,100,bitmapStream);
    Log.i(“标签”、“压缩后”);
    试一试{
    write(bitmapStream.toByteArray());
    Log.i(“标记”、“内部尝试”);
    }捕获(IOE1异常){
    Log.i(标记“无法写入文件内容”);
    }
    //创建初始元数据-MIME类型和标题。
    //请注意,用户稍后可以更改标题。
    MetadataChangeSet MetadataChangeSet=新建MetadataChangeSet.Builder()
    .setMimeType(“image/jpeg”).setTitle(“Android Photo.png”).build();
    //为文件选择器创建意图,并启动它。
    IntentSender IntentSender=Drive.DriveApi
    .newCreateFileActivityBuilder()
    .setInitialMetadata(metadataChangeSet)
    .setInitialDriveContents(结果.getDriveContents())
    .build(Bqdriveactivity.getmgoogleapClient());
    试一试{
    startinentsenderforresult(
    intentSender,请求\代码\创建者,null,0,0,0);
    }捕获(发送){
    Log.i(标记“未能启动文件选择器”);
    }
    }  
    });
    }
    受保护的void onActivityResult(最终int请求代码、最终int结果代码、最终意图数据){
    开关(请求代码){
    案例请求\代码\创建者:
    //将文件保存到驱动器后调用。
    if(resultCode==RESULT\u OK){
    Log.i(标记“图像已成功保存”);
    mbitmatosave=null;
    }
    打破
    }
    }
    }
    
    请提供一些stacktrace。您好,我自己找到了解决方案。我的连接对象在输入我指定的上述类(第三类)时未连接。它在输入onPause方法时断开连接(例如:mGoogleapiclient.disconnect())。我删除了注释,所有操作都正常。请注意,代码是在第一个类(即我的连接类)中指定的,此处未指定。在一句话中,我建议