我開發了一個應用程式,它將照片保存在應用程式檔案夾(/android/data/[app-folder]/files/Report/[directory])中,并在DESCRIPTION欄位中保存了一些資料,這是我保存影像的代碼:
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, directory);
image.put(Images.Media.DISPLAY_NAME, FOTOC_PREFIX_NAME "_" directory "_" data ".jpg");
image.put(Images.Media.DESCRIPTION, "EXAMPLE DESC");
image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
image.put(Images.Media.MIME_TYPE, "image/jpg");
image.put(Images.Media.ORIENTATION, 0);
File parent = imagePath.getParentFile();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
image.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, parent.getAbsolutePath());
}
image.put(Images.ImageColumns.BUCKET_ID, parent.toString().toLowerCase().hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName().toLowerCase());
image.put(Images.Media.SIZE, imagePath.length());
image.put(Images.Media.DATA, imagePath.getAbsolutePath());
Uri result = getContentResolver().insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), image);
然后我讀取資料:
String whereCause = MediaStore.Images.Media.DISPLAY_NAME " LIKE '%" UsefullThings.getNameWithoutExtension(photofile.getName()) "%'";
String[] projection = new String[] {MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DESCRIPTION,MediaStore.Images.Media.TITLE,MediaStore.Images.Media._ID};
Cursor cursor = getContentResolver().query(MediaStore.Images.Media
.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), projection, whereCause, null, MediaStore.Images.Media._ID);
if (cursor.getCount() == 1) {
cursor.moveToFirst();
}
String descstring = "";
try {
descstring =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DESCRIPTION));
}catch(Exception e){
....
}
其中 photofile 是影像。一切正常,但如果我重新啟動我的平板電腦,那么相同的查詢將回傳空游標。
誰能幫我?謝謝
uj5u.com熱心網友回復:
將照片保存在應用檔案夾中的應用(/android/data/[app-folder]/files/Report/[directory])
你的意思是 /storage/emulated/0/Android/data/[app-folder]/files/Report/[directory] ???
如果您使用 MediaStore,那是不可能的。
那是一個特定于應用程式的目錄,MediaStore 不會為您提供該位置的 insert() uri。
image.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, parent.getAbsolutePath());
您應該在 .RELATIVE_PATH 中放置一個相對路徑。不像你現在做的那樣完整的絕對路徑。我們也看不到你到底放了什么。請告訴 parent.getAbsolutePath() 的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/437722.html
