1

When to use

// to get image randomly
    public static Uri getRandomImage(ContentResolver resolver) {

        String[] projection = new String[] { BaseColumns._ID
        };
        Uri uri = new Random().nextInt(1) == 0 ? Media.EXTERNAL_CONTENT_URI
                : Media.INTERNAL_CONTENT_URI;

        Cursor cursor = Media.query(resolver, uri, projection, null,
                MediaColumns._ID);
        if (cursor == null || cursor.getCount() <= 0) {
            return null;
        }

        cursor.moveToPosition(new Random().nextInt(cursor.getCount()));

        return Uri.withAppendedPath(uri, cursor.getString(0));
    }

and when to use ACTION_GET_CONTENT intent ?

as I want to pick an Image from an android device !

PLease help

Rand Halim
  • 87
  • 1
  • 8

1 Answers1

0

If you want to pick an image from teh android gallery then try something like this :

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 100);

and to get back the result you could do something like this :

protected void onActivityResult(int requestCode, int resultCode, 
   Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(
                           selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    }
}

}

You could also refer to this :

How to pick an image from gallery (SD Card) for my app?

Community
  • 1
  • 1
lokoko
  • 5,785
  • 5
  • 35
  • 68