从画廊android中提取照片

时间:2014-03-13 09:37:37

标签: android

我正在使用此代码提取照片(身份证明,日期),但我不知道如何访问照片(位图)以便我可以展示它!

//提取照片的信息

 public ArrayList<Image> checkGallerieFiles(){
    String[] projection = new String[]{
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATE_TAKEN,
            MediaStore.Images.Media.DATA
    };
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    ContentResolver cr = getActivity().getContentResolver();
    Cursor cur = cr.query(uri, projection, "",null, "");
    if (cur.moveToFirst()) {
    while (cur.moveToNext()) 
    {
    Image newImage = new Image ();   

newImage.setImageName(cur.getString(cur.getColumnIndex(MediaStore.Images.Media._ID)));
newImage.setImageDate(cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN)));  
        myImageList.add(newImage);
        } 
    }
    return (myImageList);
}

1 个答案:

答案 0 :(得分:1)

这是我的代码。你得到我刚刚获取文件路径的所有图像,然后我的第二个函数将从文件路径给你图像位图。

private void getallimages(File dir)
    {

        String[] STAR = { "*" };
        controller.images.clear();

        final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
        Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        int count = imagecursor.getCount();
        for (int i = 0; i < count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            ImageItem imageItem = new ImageItem();//this is my wrapper class 
            if(new File(imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA))).length()<=10485760)
            {
                imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));

            imageItem.id = id;
            imageItem.selection = false; //newly added item will be selected by default this it do for check box unselect u dont need to fill this 
            controller.images.add(imageItem);//this i just add all info in wrapper class    

            }
  }
     }

来自filepath的getbitmap

File imgFile = new  File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    //Drawable d = new BitmapDrawable(getResources(), myBitmap);
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}