2011-12-01 101 views
12

嘿我想獲取用戶通過任何相機應用拍攝的最後一張照片。 我不知道該怎麼做獲取用戶拍攝的最後一張照片

任何人都可以幫助我嗎?

進一步我想該圖像作爲附件發送到電子郵件或彩信..

感謝

+0

我知道這是不太你問什麼,但也許它更是什麼意思?您可以啓動相機活動並獲取用戶拍攝的照片。請參閱這裏http://stackoverflow.com/questions/2314958/using-the-camera-activity-in-android – Craigy

回答

44
// Find the last picture 
String[] projection = new String[]{ 
    MediaStore.Images.ImageColumns._ID, 
    MediaStore.Images.ImageColumns.DATA, 
    MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, 
    MediaStore.Images.ImageColumns.DATE_TAKEN, 
    MediaStore.Images.ImageColumns.MIME_TYPE 
    }; 
final Cursor cursor = getContext().getContentResolver() 
     .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, 
       null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC"); 

// Put it in the image view 
if (cursor.moveToFirst()) { 
    final ImageView imageView = (ImageView) findViewById(R.id.pictureView); 
    String imageLocation = cursor.getString(1); 
    File imageFile = new File(imageLocation); 
    if (imageFile.exists()) { // TODO: is there a better way to do this? 
     Bitmap bm = BitmapFactory.decodeFile(imageLocation); 
     imageView.setImageBitmap(bm);   
    } 
} 

我還在工作的MMS發送部分。

+0

DATE_TAKEN的價值是什麼? – blackjack

+2

@blackjack它的MediaStore.Images.ImageColumns.DATE_TAKEN – jimmithy

+0

請注意,這現在已經過時:自從Honeycomb(Android 3.0)以來'managedQuery'已被棄用。 –

2

通過https://stackoverflow.com/a/20065920/763459

啓發所以在這個問題的答案主要關注的是並非所有的設備都使用「DCIM」的相機文件夾下。然後我發現如果文件位於應用程序指定的文件夾內,它將被編入索引ContentResolver,但其他應用程序無權訪問它,這意味着canRead=false。所以我在這裏提出了另一種解決方案:

while (cursor.moveToNext()) { 
     String imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)); 
     File imageFile = new File(imagePath); 
     if (imageFile.canRead() && imageFile.exists()) { 
      // we have found the latest picture in the public folder, do whatever you want 
      break; 
     } 
    } 
相關問題