任何人都可以帮助我吗?
此外,我想将该图像作为电子邮件或彩信的附件发送..
感谢
答案 0 :(得分:51)
// 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发送部分。
答案 1 :(得分:3)
受https://stackoverflow.com/a/20065920/763459启发
因此,该答案的主要关注点并非所有设备都使用“DCIM”作为相机文件夹。然后我发现如果文件位于app指定的文件夹内,它将被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;
}
}