Android Trick - Pick photo from Gallery and 3rd party app

In Android we can using Intent to share data between applications.

Simple usage for this part is Picking images or photos from the other applications.

Normally the codes is like the following:


private void pickPhotoFromGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_PICK);

    //following line code will specify the dialog title of choosing different content source app
    startActivityForResult(Intent.createChooser(intent, "Complete action using"),SELECT_PHOTO);
}

Noticed that if I set Intent Action as Intent.ACTION_PICK it will only select images from System Gallery App.

if I called 
intent.setAction(Intent.ACTION_GET_CONTENT); 
It will get from the other apps.

And the result is not same. From Gallery app the uri format is like :
content://xxx/xx/xx
but the other app will return uri like :
file:///xxx/xx/xx

Actually the Gallery app returned is not the absolute path of that image but the others app return file absolute path.

So create this kind of function to get the absolute path:

public static String getPath(Context context, Uri uri) {
    String path = uri.toString();
    if(path.startsWith("file:")){
        return path.substring(7);
    }
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(uri, projection,null, null, null);
    if (cursor == null)
        return null;
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String s = cursor.getString(column_index);
    cursor.close();
    return s;
}


Following all the codes about this part:

protected static final int SELECT_PHOTO = 100;
protected static final int CROP_PHOTO = 101;
protected static final int CAPTURE_PHOTO = 102;


private void pickPhotoFromCamera() {
try {
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(captureIntent, CAPTURE_PHOTO);
} catch (ActivityNotFoundException anfe) {
    // display an error message
    String errorMessage = "Whoops - your device doesn't support capturing images!";
    Utils.Toast(ActivityBase.this, errorMessage);
}
}

private void pickPhotoFromGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    // intent.setAction(Intent.ACTION_PICK);

    startActivityForResult(
    Intent.createChooser(intent, "Complete action using"),SELECT_PHOTO);
}

private void cropPhotoFromUri(Uri uri) {
    if(uri == null){
        return ;
    }
    MyLog.i(TAG, "select img uri:"+uri.toString());
    try {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setData(uri);
        intent.putExtra("crop", "true");
        intent.putExtra("outputX", 256);
        intent.putExtra("outputY", 256);
        if (flagSqure) {
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
    }
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);
    // intent.putExtra("noFaceDetection", true);
    // intent.putExtra("output", Uri.fromFile(someOutputFile));
    startActivityForResult(intent, CROP_PHOTO);
    } catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "Whoops - your device doesn't support the crop this image! upload directly";
        Utils.Toast(ActivityBase.this, errorMessage);
        File file = new File(Utils.getPath(ActivityBase.this, uri));
        if (photoListener != null) {
            photoListener.onPhotoSelected(file);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {

switch (requestCode) {
case SELECT_PHOTO:
    if (resultCode == RESULT_OK) {
        Uri selectedImage = imageReturnedIntent.getData();
        cropPhotoFromUri(selectedImage);
    }
    break;
case CAPTURE_PHOTO:
if (resultCode == RESULT_OK) {
    Uri selectedImage = imageReturnedIntent.getData();
    MyLog.i(TAG, "get img url:" + selectedImage.getPath());
    cropPhotoFromUri(selectedImage);
}
break;
case CROP_PHOTO:
    if (resultCode == RESULT_OK) {
        MyLog.i(TAG, "" + imageReturnedIntent.getData());
        Bundle extras = imageReturnedIntent.getExtras();
        Bitmap bmp = extras.getParcelable("data");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bmp.compress(CompressFormat.JPEG, 75, bos);
        byte[] data = bos.toByteArray();
        if (photoListener != null) {
            photoListener.onPhotoSelected(data);
        }
    }
    break;
}
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
}

Comments

Popular posts from this blog

A wired issue of MediaPlayer on Android

Problem when using Jackson json parser in Android.

Gradle issue - peer not authenticated