我正面临着从Android相机拍摄照片然后将其显示给下一个活动的问题。过程工作正常,但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) {
}
答案 0 :(得分:0)
我最后解决了这个问题:
我已经在互联网上搜索过这个问题,实际上这些手机没有使用intent.get(“数据”)获取图像,这就是我使用上述技术的原因。我试过这个使用三星王牌手机,三星Galaxy和同样的问题是有的。我猜整个三星都没有完美的工作,当你需要一次又一次地拍照时,延迟响应就在那里:(像htc这样的其他手机效果很好。