![](/img/trans.png)
[英]How can I open an image saved within the app with the android gallery?
[英]Android Gallery not refreshed after Image is saved
我正在嘗試保存圖像,並提醒圖庫有關新圖像的信息。 該圖像不會立即顯示在圖庫中,但刷新可能需要幾個小時。 我使用以下代碼:
String title = "title";
String r = MediaStore.Images.Media.insertImage(getContentResolver(), State.pictureWithDekor, title, title);
Uri uri = Uri.parse(r);
MediaScannerConnection.scanFile(this,
new String[] { uri.getPath() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("TAG", "Finished scanning " + path);
}
});
insertImage
之后的r
值為content://media/external/images/media/2082
,onScanCompleted中的uri
為null
。
我究竟做錯了什么?
問題是從MediaStore.Images.Media.insertImage
返回的路徑不是物理路徑。 要獲取真實路徑,請使用:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.