这是我录制视频并将其保存到驱动器的代码。但我的日志显示如下:
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");
}
我正在创建Async类的实例:
@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 :(得分: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
});
答案 1 :(得分:0)
我通过声明doInBackground和onPostExecute同步来解决一个非常类似的问题
(...)
protected synchronized void doInBackground(Void ... params)
{...}
protected synchronized void onPostExecute(布尔结果){...}
(...)
在我的例子中,doInBackground()首先进入(因为它应该),但在doInBackground()&#34;技术上&#34;之前调用onPostExecute()。完成(我无法弄清楚为什么 - 也许我的解压缩库对象有自己的线程并且没有阻止doInBackground()方法)。同步方法解决了问题(当然,并非没有性能影响)