我的MediaScanner有问题。我正在使用原生相机应用拍照,我想将ImageView设置为此图像。当我从图库中选择一张图片时,它可以正常工作,但是我无法让应用程序等待MediaScanner完成对我新拍摄图像的扫描。这是我的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action
.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
} else {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = this.getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
selectedImageUri = Uri.parse(cursor.getString(columnIndex));
cursor.close();
}
// The app is not waiting for these lines,
// I have also tried them alone each of them.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
outputFileUri));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_FINISHED,
outputFileUri));
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImageUri.toString(), options);
options.inSampleSize = calculateInSampleSize(options,
dpToPx(100), dpToPx(100));
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bMapRotate = BitmapFactory.decodeFile(
selectedImageUri.toString(), options);
int width = bMapRotate.getWidth();
int height = bMapRotate.getHeight();
pic1 = (ImageView) findViewById(R.id.ivPic1);
int viewHeight = pic1.getLayoutParams().height;
int viewWidth = pic1.getLayoutParams().width;
if (width > height)
viewHeight = height;
else
viewWidth = width;
if (bMapRotate != null) {
pic1.setImageBitmap(bMapRotate);
}
}
}
}
我也尝试过这种方式:
MediaScannerConnection.scanFile(this,
new String[] { outputFileUri.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
setImage(selectedImageUri);
}
});
的内心
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
outputFileUri));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_FINISHED,
outputFileUri));
然后在setImage方法中使用setImage代码,该代码应在扫描完成时调用,但它仍然不等待扫描完成?它只是一个文件被扫描,所以我不认为它应该是那么复杂?我应该使用AsyncTask还是您还有其他建议吗?
修改 事实证明,这不是问题所在。问题是Uri有"文件:/"在路径前面,这不是其他代码应该如何阅读。