【发布时间】:2012-07-20 10:42:39
【问题描述】:
我面临从安卓相机拍照然后将其显示给下一个活动的问题。过程正常,但在 10 张图片中,有 1 张图片丢失。
步骤:我调用相机并拍照的活动 a,然后我将其 URI 的活动 b 传递给在图像视图中显示,重复此循环 10 次 1-2 次图片丢失,并且在图片时观察到 2 秒的延迟被拍摄并以空白屏幕的形式显示到下一个屏幕的图像视图中。
请检查代码并指导我在这里犯了什么错误?
活动 A
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_IMAGE_CAPTURE && resultCode==Activity.RESULT_OK){
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA};
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's
MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
//At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable
Cursor myCursor = this.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);
long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";
try{
myCursor.moveToFirst();
imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
}
finally{myCursor.close();}
//Create new Cursor to obtain the file Path for the large image
String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA
};
String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
myCursor = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
String largeImagePath = "";
try{
myCursor.moveToFirst();
//This will actually give you the file path location of the image.
largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
}
finally{myCursor.close();}
// These are the two URI's you'll be interested in. They give you a handle to the actual images
Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));
Intent next= new Intent(this,AddListing.class);
next.putExtra("imageUriThumb", uriThumbnailImage.toString());
next.putExtra("imageUriFull", uriLargeImage.toString());
startActivity(next);
}
}
活动 B
String uri = bundle.getString("imageUriThumb");
Uri myThumbUri = Uri.parse(uri);
try {
tempBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myThumbUri);
imgThumbnail.setImageBitmap(tempBitmap);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
tempBitmap.compress(Bitmap.CompressFormat.PNG, 90, bao);
byte [] ba = bao.toByteArray();
imageBytesString = Base64.encodeToString(ba, 0);
} catch (Exception e) {
}
【问题讨论】:
-
您在哪个设备上测试?
-
ASIAK 当您使用本机应用程序捕获图像时,Samsung Galaxy OS 存在一些问题,它不会返回捕获图像的 URI。你能在互联网上搜索更多相同的问题吗?
-
我已经在互联网上搜索过这个问题,实际上这些手机没有使用 intent.get("data") 获取图像,这就是我使用该技术的原因。我用三星王牌手机试过这个,同样的问题也存在。我想整个三星都不好:( HTC 等其他手机都可以完美运行。
标签: android camera imageview uri