fetching images from gallery on android phones with internal storage

微笑、不失礼 提交于 2019-12-05 05:05:31

Try this on your button like "Browse"

   browse.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

         Intent i = new Intent(
                 Intent.ACTION_PICK,
                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                 startActivityForResult(i, RESULT_LOAD_IMAGE);

    }
});

and you cal also set selected image into your ImageView as

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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

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

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

             imageView = (ImageView) findViewById(R.id.property_image);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

}

In first block of code i use startActivityForResult(i, RESULT_LOAD_IMAGE); this return result to called activity and we can get this result by second block of code, and set selected image in your ImageView

String[] projection = {MediaStore.Images.Media._ID};

Replace MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI with MediaStore.Images.MEDIA.EXTERNAL_CONTENT_URI

   Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,       
                null);


    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    int size = cursor.getCount();
    // If size is 0, there are no images on the SD Card.
    if (size == 0) 
    {
        Log.e("size 0","0");
    }

EDIT: Get the list of thumbnails and get the image URI from the cursor


Uri uri=MediaStore.Images.Thumbnails.getContentUri("external");

 Cursor cursor=MediaStore.Images.Thumbnails.queryMiniThumbnails
      (getContentResolver(), uri, MediaStore.Images.Thumbnails.MINI_KIND,null);

if( cursor != null && cursor.getCount() > 0 ) {
    String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
 }
Arun C

To get list of Gallery images you can try this

String[] projection = new String[]{
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.Images.Media.DATE_TAKEN
};


Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;


Cursor cur = getContentResolver().query(imageUri,
        projection, // Which columns to return
        null,       // Which rows to return (all rows)
        null,       // Selection arguments (none)
        null        // Ordering
        );

Log.i("Images Count"," count="+cur.getCount());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!