0

Using code from this answer: https://stackoverflow.com/a/12347567/734687

I use the following code to let a user submit a picture to my Android application.

private void openImageIntent() {
    File outputFile = null;
    try {
        outputFile = File.createTempFile("tmp", "face", getCacheDir());
    } catch (IOException pE) {
        pE.printStackTrace();
    }
    mOutputFileUri = Uri.fromFile(outputFile);

    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for(ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutputFileUri);
        cameraIntents.add(intent);
    }

    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_PICK);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    startActivityForResult(chooserIntent, 42); //XXX 42?
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 42) {
            final boolean isCamera;
            if (data == null) {
                isCamera = true;
            } else {
                final String action = data.getAction();
                if (action == null) {
                    isCamera = false;
                } else {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }
            Uri selectedImageUri = null;
            if (isCamera) {
                selectedImageUri = mOutputFileUri;
            } else {
                selectedImageUri = data == null ? null : data.getData();
            }
            loadFacePicture(selectedImageUri);
        }
    }
}

However the Uri selectedImageUri leads to an empty file on loadFacePicture(selectedImageUri);

What did I missed ?

Community
  • 1
  • 1
Antzi
  • 11,625
  • 6
  • 40
  • 66
  • What value getting in `String action = data.getAction();` ? – ρяσѕρєя K Jun 29 '16 at 04:56
  • @ρяσѕρєяK actually data is null. resultCode is RESULT_OK and requestCode 42 tho – Antzi Jun 29 '16 at 05:01
  • I recommend you to check this answer: http://stackoverflow.com/a/36394744/361100 – Youngjae Jun 29 '16 at 06:09
  • @Youngjae In my case, I get both no data and an invalid URI... sounds kind of like a dead end... – Antzi Jun 29 '16 at 06:20
  • @Antzi // How about to remove `if (resultCode == RESULT_OK)`? I tested your code, and Galleray app gives uri well. But default Camera app gives resultCode as `RESULT_CANCELED` after I captured from Camera. – Youngjae Jun 29 '16 at 08:55
  • @Antzi // I cannot describe exactly but your implementation using `chooserIntent` cause malfunction against Camera app. how about not to use `queryIntentActivities` and simply call intent as this method? http://stackoverflow.com/q/36394526/361100 – Youngjae Jun 29 '16 at 09:49

1 Answers1

1

This is what I endup doing. This seemed to be the source of the issue:

    intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutputFileUri);

The actual code:

private void openImageIntent() {
        try {
            outputFile = File.createTempFile("tmp", ".jpg", getCacheDir());
        } catch (IOException pE) {
            pE.printStackTrace();
        }
        mOutputFileUri = Uri.fromFile(outputFile);

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_PICK);

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

        startActivityForResult(chooserIntent, 42);
    }


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 42) {
                Bitmap bmp = null;
                if (data.hasExtra("data")) {
                    Bundle extras = data.getExtras();
                    bmp = (Bitmap) extras.get("data");
                } else {
                    AssetFileDescriptor fd = null;
                    try {
                        fd = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
                    } catch (FileNotFoundException pE) {
                        pE.printStackTrace();
                    }
                    bmp = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
                }
                try {
                        FileOutputStream out = new FileOutputStream(new File(mOutputFileUri.getPath()));
                        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
                        out.flush();
                        out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                loadFacePicture(mOutputFileUri);
            }
        }
    }
Antzi
  • 11,625
  • 6
  • 40
  • 66