Fetch all Images from Android Photo Gallery

后端 未结 2 793
Happy的楠姐
Happy的楠姐 2021-01-07 12:13

I want to fetch all images in my application from android photo gallery .because I want to load an image into my application again after saving it into android gallery . For

相关标签:
2条回答
  • 2021-01-07 13:12

    by using this code i am able to display phone gallery images in gridview, depending on your requirement change this..

    public class MyPhoneGallery extends Activity  {
    
      /** Called when the activity is first created. */
      private Cursor imagecursor, actualimagecursor;
      private int image_column_index, actual_image_column_index;
      GridView imagegrid;
      private int count;
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gallery);
            init_phone_image_grid();
      }
      private void init_phone_image_grid() {
            String[] img = { MediaStore.Images.Thumbnails._ID };
            imagecursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
            image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
            count = imagecursor.getCount(); 
            imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
            imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
            imagegrid.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
    
                  System.gc();
                  String[] proj = { MediaStore.Images.Media.DATA };
                  actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,null, null, null);
                  actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                  actualimagecursor.moveToPosition(position);
                  String i = actualimagecursor.getString(actual_image_column_index);
                  System.gc();
                  Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                  intent.putExtra("filename", i);
                  startActivity(intent);
    
               }
          });
      }
    
      public class ImageAdapter extends BaseAdapter {
            private Context mContext;
            public ImageAdapter(Context c) {
                  mContext = c;
             }
            public int mygetItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }
            public int getCount() {
                  return count;
            }
            public Object getItem(int position) {
                  return position;
            }
            public long getItemId(int position) {
                  return position;
            }
            public View getView(int position,View convertView,ViewGroup parent) {
                  System.gc();
                  ImageView i = new ImageView(mContext.getApplicationContext());
                  if (convertView == null) {
                        imagecursor.moveToPosition(position);
                        int id = imagecursor.getInt(image_column_index);
                        i.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+ id));
                        i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        i.setLayoutParams(new GridView.LayoutParams(125, 125));
                  }
                  else {
                        i = (ImageView) convertView;
                  }
                  return i;
            }
      }
    }
    
    0 讨论(0)
  • 2021-01-07 13:18

    I used this code and got image, maybe it works for you

    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media._ID;
    imagecursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
    
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
    
    this.count = imagecursor.getCount();
    this.arrPath = new String[this.count];
    ids = new int[count];
    for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            ids[i] = imagecursor.getInt(image_column_index);
            int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
            arrPath[i] = imagecursor.getString(dataColumnIndex);
        }
    

    You can find adapter code gridview and setadapter from below URL source code for adapter and gridview

    0 讨论(0)
提交回复
热议问题