我把它设置为拍照,保存,然后上传图片。我的问题是 MediaScannerConnection 在尝试上传图片之前没有完成。我需要一些不同类型的返回语句吗?我怎样才能让它等待?
编辑以包含完整代码。
public void onClick(View v) {
if (v.getId() == R.id.capture_btn) {
String fileName = System.currentTimeMillis() + ".jpg";
String path1 = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + "Photos App";
try {
path = new File(path1);
if (!path.exists()) {
path.mkdirs();
}
} catch (Exception e) {
Log.d("creating file error", e.toString());
}
photo = new File(path, fileName);
Log.d("main screen", "photo = " + photo);
Intent cameraintent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
startActivityForResult(cameraintent,
CAMERA_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_IMAGE_CAPTURE
&& resultCode == Activity.RESULT_OK) {
scanMedia();
if(scanned){
new PostPicture().execute();
}
}
}
private File scanMedia() {
// TODO Auto-generated method stub
MediaScannerConnection.scanFile(MainScreen.this,
new String[] { photo.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
scanned = true;
}
});
return path;
}
class PostPicture extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();