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) {
}