1

我想从 android 照片库中获取我的应用程序中的所有图像。因为我想在将图像保存到 android 库后再次将其加载到我的应用程序中。为了将我的图像保存到图库中,我使用以下方法。

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

现在,我想以编程方式将所有图像提取到我的应用程序中,并希望在 tile 的基础上获得高于保存的图像。这里有两个问题。1st:我可以从 android 的照片库中获取所有图像吗?第二:我可以在获取后读取单张照片的详细信息,例如标题等。

4

2 回答 2

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;
        }
  }
}
于 2012-08-25T12:01:42.170 回答
0

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

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 的 URL 源代码中找到适配器代码 gridview 和 setadapter

于 2014-09-06T06:13:22.803 回答