1

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

2 回答 2

2

The answer was to put the async task in a runnable. I found out I couldn't try to load the async task outside of the main UI thread. Also the code below makes it wait until it finishes scanning.

    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;
                }
于 2013-11-02T21:01:32.213 回答
0

从您的代码中无法很好地理解它,我认为您需要添加其余部分。

但是,试试这个:

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;
        if(scanned){
        new PostPicture().execute();
        } else {
Log.e("Should scan","Didn't finish scanning")
}
                }
            });
    return path;
}

并删除

    if(scanned){
    new PostPicture().execute();
于 2013-11-01T19:16:02.600 回答