I'm calling the default camera from my activity and then handling the onActivityResult. My code seems to work fine on the LG Ally which doesn't have a confirmation when a picture is taken. However, when I run the same app on the Nexus S, it prompts me with an "Ok", "Retake", or "Cancel" before returning to my activity. While "Cancel" works, returning to my activity without saving the picture, "Ok" doesn't seem to have any effect, not even returning to my activity.

My code below:

private void captureImage() {

    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + (new UserContextAdapter(this)).getUser() + "/");
        path.mkdirs();
        File file = new File(path, "Image_Story_" + mRowId.toString() + ".jpg");

        newImageUri = Uri.fromFile(file);

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);

        startActivityForResult(intent, CAPTURE_IMAGE);
    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    switch (requestCode) {
    case CAPTURE_IMAGE:
        switch (resultCode ) {
        case 0:
            Log.i("CAPTURE", "Cancelled by User");
            break;
        case -1:
            mImageUri = newImageUri;
            setImageFromUri();
            }
    }
有帮助吗?

解决方案

I think I just had the exact same problem.

If the path to save the picture isn't correct, the camera won't return to your app. Once I made sure the directory exists, everything worked fine. Make sure the directory exists, then it should work.

-- Edit --

I just saw, that you call path.mkdirs(); but I think that it doesn't work. As you can read in the android doc "Note that this method does not throw IOException on failure. Callers must check the return value.". Please be sure to check if the directory really exists.

hth

其他提示

Also, make sure your application has <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> if you're using Environment.getExternalStorageDirectory().getPath() above.

Hope this helps =)

please check this

Case 1:

Uri newImageUri = null;

File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/");

path.mkdirs();

boolean setWritable = false;

setWritable = path.setWritable(true, false);

File file = new File(path, "Image_Story_" + System.currentTimeMillis() + ".jpg");

newImageUri = Uri.fromFile(file);

Log.i("MainActivity", "new image uri to string is " + newImageUri.toString());

Log.i("MainActivity", "new image path is " + newImageUri.getPath());            

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, newImageUri);

startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);

Case 2:

String fileName = "" + System.currentTimeMillis() + ".jpg";

ContentValues values = new ContentValues();

values.put(MediaStore.Images.Media.TITLE, fileName);

values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");

values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Log.i("MainActivity", "new image uri to string is " + imageUri.toString());

Log.i("MainActivity", "new image path is " + imageUri.getPath());

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);

I am able to save images through camera on nexus s in both of the above cases In case 1: a.Image is stored in custom folder. b. If the “System.currentTimeMillis()” is changed to (“new Date().toString()”) image is not saved and camera does not return to my activity. (Probably because “System.currentTimeMillis” has no spaces and “new Date().toString()” might be having some special characters and spaces) In case 2: a. Image is stored in camera folder

Thanks to all

I have same problem.You have to do only one job,in your Phone storage check that you have Pictures directory or not.If you don't have such library then make it manually.

Hope this works for you.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top