fetching images from gallery on android phones with internal storage

ε祈祈猫儿з 提交于 2020-01-23 05:54:54

问题


Hi I am developing an Android Gallery app where I am fetching images from built in gallery and displaying it.I am using the code as below

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


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


        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._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");
        }

The Problem is when I run this code on Phones having only internal storage (Galaxy Nexus) I am getting Log which says size as Zero even though there are images in built in gallery. How do I resolve this. Please Help.Thanks!


回答1:


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




回答2:


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());



回答3:


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 ) );
 }


来源:https://stackoverflow.com/questions/18225178/fetching-images-from-gallery-on-android-phones-with-internal-storage

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