Pergunta

Another weird question. Always getting RESULT_CANCELLED (that's a problem) while working with camera via intent. More relevant context:

I assign setOnClickListener to ImageView in activities' onCreate:

this.imSnapshot.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
});

Code of getImageUri method:

private Uri getImageUri() {
    ContextWrapper cw = new ContextWrapper(this);
    File dir = cw.getDir("dcim", Context.MODE_PRIVATE);
    File file = new File(dir, UUID.randomUUID().toString()+".png");
    Uri imgUri = Uri.fromFile(file);
    Log.d("DetailsModify.getImageUri", imgUri.toString());
    return imgUri;
}

Here is onActivityResult (log line always return 0):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        Log.d("DetailsModify", "onActivityResult code " + resultCode);
        if (resultCode == RESULT_OK) {

UPDATE: the error was filtered by IDE so I did not notice it originally:

CameraActivity:Saved image not found
        java.io.FileNotFoundException
        at android.os.Parcel.openFileDescriptor(Native Method)
        at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:119)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:481)
        at android.content.ContentResolver.openOutputStream(ContentResolver.java:365)
        at android.content.ContentResolver.openOutputStream(ContentResolver.java:341)
        at com.sonyericsson.android.camera.CameraActivity.copyContentToExtraOutputUri(CameraActivity.java:1680)
        at com.sonyericsson.android.camera.CameraActivity.setResult(CameraActivity.java:1639)
        at com.sonyericsson.android.camera.view.CameraWindow.onStoreComplete(CameraWindow.java:1405)
        at com.sonyericsson.android.camera.PhotoController.onStoreComplete(PhotoController.java:879)
        at com.sonyericsson.android.camera.TakenStatus.notifyComplete(TakenStatus.java:83)
        at com.sonyericsson.android.camera.ImageManager$StoreDataHandler.handleMessage(ImageManager.java:960)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:138)
        at android.app.ActivityThread.main(ActivityThread.java:3701)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
        at dalvik.system.NativeStart.main(Native Method)
Foi útil?

Solução

As I have not received any better solution to my question, sharing what I got on my own. I made my problem solved by adding dir.setWritable(true, false) to method below, tested on API10:

private Uri getImageUri() {
    ContextWrapper cw = new ContextWrapper(this);
    File dir = cw.getDir("dcim", Context.MODE_PRIVATE);
    dir.setWritable(true, false);
    File file = new File(dir, UUID.randomUUID().toString()+".png");
    Uri imgUri = Uri.fromFile(file);
    Log.d("DetailsModify.getImageUri", imgUri.toString());
    return imgUri;
}

What it does is it sets permissions allowing Camera to write to application's private location (more info at http://developer.android.com/reference/java/io/File.html#setWritable%28boolean,%20boolean%29). I hope that helps someone.

Outras dicas

To NeviQ-Or's comment:

If

    dir.setWritable(true, false);

does not work, try using

    "absolute_file_path".setWritable(true, false);

The former did not work for me, but the latter did.

My problem was with the theme of the activity.. I tried everything and nothing worked, I was testing with android 11... so I fixed it by removing the following code from the manifest..

android:theme="@style/FullscreenTheme"

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top