我正在按照Google的官方文档编写相机和图库应用。问题在于,它主要侧重于如何拍照以及如何将其保存在内存中。我通过getExternalFilesDir()将它们保存在应用程序的外部存储器中。
我现在想要做的是通过MediaStore来简单地查询保存了照片的目录,以便在回收者视图中显示其中的所有照片。但是我似乎找不到任何有关如何进行检索的解释。 Uri出了点问题,我可能不太了解,也不知道如何解决。即使在https://developer.android.com/training/data-storage/files#PrivateFiles中,该文章也只讨论将文件保存在各种存储选项中,而不是检索它们。
这是我保存文件的代码:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
这是实际发送意图的函数(然后onActivityResult处理返回的数据):
private void dispatchTakePictureIntent() {
final String TAG = "dispatchTakePic: ";
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Error while creating a file");
ex.printStackTrace();
}
// Continue only if the File was successfully created
// Authority has to be exactly like in manifest
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
这是我尝试获取有关所拍摄照片的信息的代码:
private void populatePhotoArray() {
final String TAG = "popPhotoArray: ";
ContentResolver contentResolver = getContentResolver();
String photo_id, photo_title, photo_path = "";
final String[] PHOTOGRAPHS_PROJECTION = {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.DATA};
File photographsDirectory = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
Uri photographsUri = Uri.fromFile(photographsDirectory);
//Uri photographsUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
//Uri photographsUri = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
String sortOrder = MediaStore.Images.Media.DATE_ADDED + " DESC";
final Cursor cursor = contentResolver.query(
photographsUri,
PHOTOGRAPHS_PROJECTION,
null,
null,
sortOrder);
Log.d(TAG, "created the projection");
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
photo_id = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID));
photo_title = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE));
photo_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// save to photo list
applicationPhotosList.add(new Photographs(photo_id, photo_title, photo_path));
Log.d(TAG, "new photo added");
}
cursor.close();
Log.d(TAG, "Photo array filled up");
}
else if (cursor == null){
Log.d(TAG, "No photos present, cursor is null");
Toast.makeText(this, "No photos present", Toast.LENGTH_SHORT).show();
}
似乎试图将getExternalFileDir()的路径转换为Uri无效。光标为空。但是MediaStore仅具有MediaStore.Images.Media.EXTERNAL_CONTENT_URI,这不适合查询整个设备的内存,而我只需要该应用程序在其中保存所拍摄图片的特定目录。我想念什么?不会那么难,对吧?
答案 0 :(得分:0)
还有另一种方法可以不使用光标从特定文件夹中检索图像,我认为它更快。
private fun getAllShownImagesPath(): ArrayList<String> {
var filePath: ArrayList<String> = ArrayList<String>()
val path = File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path, "BacFo Camera")
if (path.exists()) {
for (i in path.list().iterator()) {
filePath?.add("" + path.toString() + "/" + i)
}
}
return filePath!!
}
` 在“文件名”中,您将拥有所有图像或视频的路径,无论保存在特定文件夹中的是什么