Question

I have it set to take a picture, save it, and then upload the picture. My problem is MediaScannerConnection doesn't finish before it tries to upload the picture. Do I need some different type of return statement? How can I make it wait?

Edited to include full code.

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();
Was it helpful?

Solution

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;
                }

OTHER TIPS

couldn't understand it well from your code, I think you need to add the rest.

but, try this:

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;
}

and remove the

    if(scanned){
    new PostPicture().execute();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top