Fetch all Images from Android Photo Gallery

后端 未结 2 794
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;
            }
      }
    }
    

提交回复
热议问题