onPostExecute()被调用时没有完成doInBackground()中的任务
这是我录制视频并将其保存到驱动器的代码。但我的日志显示如下: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);
}
问题是这样的
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
});
请您详细说明 –
'setResultCallback()'。你正在设置回调。但回调不是直接执行的。只是晚得多。但newDriveContents立即返回。因此,您的doInBackground完成并调用onPostExecute。之后 - 稍后 - 调用回调。 – greenapps
那么解决方案是什么? –
东西就刚刚与的AsyncTask做路程,只需调用的代码。 – greenapps