0

I'm trying to take an Image from any source (like dropbox, gallery, camera, retrica etc..),getting his path and setting it on a ImageView. by using this intent

  private void cameraIntent() {
      Intent pickIntent = new Intent();
        pickIntent.setType("image/*");
        pickIntent.setAction(Intent.ACTION_GET_CONTENT);

        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


        String pickTitle = "Select or take a new Picture";
        Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
        chooserIntent.putExtra
                (
                        Intent.EXTRA_INITIAL_INTENTS,
                        new Intent[]{takePhotoIntent}
                );

        startActivityForResult(chooserIntent, REQUEST_CAMERA);

    }

But now, I can only handle Gallery and Camera intents, how can I handle other apps?

1 Answer 1

0

For picking image from Camera/Gallery/DropBox OR any other File systems from the device just call implicit intent...

Following code may helps you..

pickbtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v){
            if (Environment.getExternalStorageState().equals("mounted")){
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_PICK);
                startActivityForResult(Intent.createChooser(intent, "Select Picture:"), Constants.PICK_IMAGE_FROM_LIBRARY);
            }
        }
    });

Now use OnActivity result for getting data...

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

    if(requestCode == Constants.PICK_IMAGE_FROM_LIBRARY)
    {
        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            String selectedImagePath = getPath(selectedImageUri);
            mImagePath = selectedImagePath;
            Bitmap photo = getPreview(selectedImagePath);
            mImageViewProfileImage.setImageBitmap(photo);
        }
    }
public String getPath(Uri uri)
{
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

public Bitmap getPreview(String fileName)
{
    File image = new File(fileName);

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(image.getPath(), bounds);

    if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) 
    {
        return null;
    }
    int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight : bounds.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / 64;
    return BitmapFactory.decodeFile(image.getPath(), opts);
}
}

This is a full example: https://gist.github.com/felixgborrego/7943560

1
  • This example handles only gallery
    – Spidersaw
    Jun 28, 2015 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.