2017-03-09 81 views
-3

這是我錄製視頻並將其保存到驅動器的代碼。但我的日誌顯示如下:onPostExecute()被調用時沒有完成doInBackground()中的任務

03-09 08:43:55.005 12760-12760/classroom.shivani.upload I/drive-quickstart: API client connected. 
03-09 08:43:55.005 12760-12760/classroom.shivani.upload I/drive-quickstart: Starting camera Intent 
03-09 08:43:55.014 12760-12760/classroom.shivani.upload I/drive-quickstart: file uri is :file:///storage/emulated/0/DCIM/VID_20170309_084355.mp4 
03-09 08:43:55.052 12760-12760/classroom.shivani.upload I/drive-quickstart: calling rccv 
03-09 08:43:55.053 12760-12760/classroom.shivani.upload I/drive-quickstart: In pre execute 
03-09 08:43:55.057 12760-13801/classroom.shivani.upload I/drive-quickstart: In background 
03-09 08:43:55.058 12760-13801/classroom.shivani.upload I/drive-quickstart: file path is :/storage/emulated/0/DCIM/VID_20170309_084355.mp4 
03-09 08:43:55.058 12760-13801/classroom.shivani.upload I/drive-quickstart: file name is :VID_20170309_084355.mp4 
03-09 08:43:55.175 12760-12760/classroom.shivani.upload I/drive-quickstart: In post execute 

我應該怎麼做才能將視頻保存到硬盤上?

private class Async extends AsyncTask<File,Void,Void>{ 

     @Override 
     protected void onPreExecute() 
     { 
      super.onPreExecute(); 
      Log.i(TAG, "In pre execute"); 
     } 
    protected Void doInBackground(File... params) 
      { 
       Log.i(TAG, "In background"); 
       final File file1=params[0]; 

       Log.i(TAG, "file path is :"+file1.getPath()); 
       Log.i(TAG, "file name is :"+file1.getName()); 

       Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 
        @Override 
        public void onResult(DriveApi.DriveContentsResult result) { 
         if (!result.getStatus().isSuccess()) { 
          Log.i(TAG, "Failed to create new contents."); 
          return; 
         } 
         Log.i(TAG, "New contents created."); 
         OutputStream outputStream = 
           result.getDriveContents() 
             .getOutputStream(); 
         FileInputStream fis; 
         try { 

          Log.i(TAG, "Within try catch"); 

          fis = new FileInputStream(file1.getPath 
            ()); 
          Log.i(TAG, "file name is :"+file1.getName()); 
          ByteArrayOutputStream baos = new 
            ByteArrayOutputStream(); 
          byte[] buf = new byte[102400]; 
          int n; 
          while (-1 != (n = fis.read(buf))) 
           baos.write(buf, 0, n); 
          byte[] photoBytes = baos.toByteArray(); 
          outputStream.write(photoBytes); 

          outputStream.close(); 

          fis.close(); 
          Log.i(TAG, "successfully created video file"); 
          Log.i(TAG, "Setting Metadata"); 
          String title = file1.getName(); 
          MetadataChangeSet metadataChangeSet = 
            new MetadataChangeSet.Builder() 
              .setMimeType("video/mp4").setTitle 
              (title).build(); 

          Log.i(TAG, "Creating new video on Drive (" + title 
            + ")"); 
          IntentSender intentSender = Drive.DriveApi 
            .newCreateFileActivityBuilder() 
            .setInitialMetadata(metadataChangeSet) 
            .setInitialDriveContents(result.getDriveContents()) 
            .build(mGoogleApiClient); 
          try { 
           startIntentSenderForResult(
             intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0); 
          } catch (IntentSender.SendIntentException e) { 
           Log.i(TAG, "Failed to launch file chooser."); 
          } 


          Log.i(TAG, "congrats new video created with name (" + title + ")"); 

         } catch (FileNotFoundException e) { 
          Log.w(TAG, "FileNotFoundException: " 
            + e.getMessage()); 
         } catch (IOException e1) { 
          Log.w(TAG, "Unable to write file contents." + e1.getMessage()); 
         } 
         // Create an intent for the file chooser, and start it. 


        } 
       }); 
      return null; 
      } 

protected void onPostExecute(Void result) 
     { 
      super.onPostExecute(result); 
      Log.i(TAG, "In post execute"); 


     } 

我創建異步類的實例爲:

@Override 
    public void onConnected(Bundle connectionHint) { 
     Log.i(TAG, "API client connected."); 
     Log.i(TAG, "Starting camera Intent"); 
     String mediaStorageDir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath(); 
     String timeStamp=new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); 
     fileUri = Uri.fromFile(new java.io.File(mediaStorageDir + java.io.File.separator +"VID_"+timeStamp + ".mp4")); 
     file=new File(fileUri.getPath()); 
     Log.i(TAG, "file uri is :"+fileUri); 
     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE); 
     cameraIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,15); 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri); 
     startActivity(cameraIntent); 
     Log.i(TAG, "calling rccv"); 
     new Async().execute(file); 

    } 
+0

東西就剛剛與的AsyncTask做路程,只需調用的代碼。 – greenapps

回答

1

問題是這樣的

protected Void doInBackground(File... params) { 

    Log.i(TAG, "In background"); 
    final File file1=params[0]; 

    Log.i(TAG, "file path is :"+file1.getPath()); 
    Log.i(TAG, "file name is :"+file1.getName()); 

    Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 
     // all the other stuff 
    }); 

    return null; 
} 

你基本上是直吹通過doInBackground()並返回NULL直線距離,爲方法不會等待DriveApi執行任何操作。這不是應該如何使用異步任務。

剛剛嘗試調用這個主線程,它更可能已經是一個異步任務或類似

Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 
    // all the other stuff 
}); 
+0

請您詳細說明 –

+0

'setResultCallback()'。你正在設置回調。但回調不是直接執行的。只是晚得多。但newDriveContents立即返回。因此,您的doInBackground完成並調用onPostExecute。之後 - 稍後 - 調用回調。 – greenapps

+0

那麼解決方案是什麼? –

相關問題