gpt4 book ai didi

android - MediaScannerConnection 在上传新图片之前未完成

转载 作者:行者123 更新时间:2023-11-29 21:29:54 28 4
gpt4 key购买 nike

我设置为拍照,保存,然后上传图片。我的问题是 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();

最佳答案

答案是将异步任务放入可运行的。我发现我无法尝试在主 UI 线程之外加载异步任务。下面的代码也让它等待直到完成扫描。

    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);
fullPath = path;
scanned = true;
if(scanned){
new Thread()
{
public void run()
{
MainScreen.this.runOnUiThread(new Runnable()
{
public void run()
{
new PostPicture().execute();
}
});
}
}.start();
} else {
Log.e("Should scan","Didn't finish scanning");
}
}


});
return path;
}

关于android - MediaScannerConnection 在上传新图片之前未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19733770/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com