2014-03-13 104 views
0

我使用此代碼提取照片(ID,日期),但我不知道如何訪問照片(位圖),以便我可以顯示它!從圖庫中提取照片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

這是我的代碼u得到的所有圖像,我只是取文件的路徑,然後我的第二個功能會給U影像bitmao從文件路徑:) 希望這將有助於ü

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  

      } 
    } 
    } 

從文件路徑

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

} 
+0

getbitmap那正是我期待的! thkx – Marya

+0

永遠歡迎@Marya你從哪裏來的俄羅斯? –

+0

不,我是意大利人 – Marya