取从Android的照片库的所有图片(Fetch all Images from Android P

2019-06-27 09:29发布

我想。因为我想将图像保存成Android图库后再次加载到我的应用程序获取从Android照片库中我的应用程序的所有图像。 救了我的形象进入画廊我用下面的方法。

MediaStore.Images.Media.insertImage(ContentResolverObj, bitMap, "uniqueTitle", "description"); 

现在编程我想所有的图像读取到我的应用程序,并希望得到上面保存的图像文件tile.Two问题的基础上这里。 1:我可以获取从Android照片库中的所有图像? 第二:我可以读取它后读取一张照片的细节如标题等。

Answer 1:

通过使用此代码,我能够在GridView控件来显示手机的画廊图像,这取决于你的要求改变这种..

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


Answer 2:

我用这个代码,并得到了图像,也许对你有用

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

你可以找到适配器代码GridView和setadapter从下面网址源代码适配器和GridView



文章来源: Fetch all Images from Android Photo Gallery