正如标题所述,我正在应用程序指定的外部目录(android / data / com.xxx.xxx)中保存一些文件-大多数是音频文件-现在我想使用Mediastore访问这些音频文件,但我可以我该怎么办? 媒体商店可以访问这种私有目录吗?
值得一提的是,整个数据目录都有一个.nomedia文件
更新:
文件保存在... / android / data / com.xxx.xxx / files / music getExternalFilesDir
存储文件中
mediastore找到其他音频文件,但无法访问该目录中的文件
context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
BASE_PROJECTION, selection, selectionValues, sortOrder);
UPDATE2: 创建文件后使用以下扫描
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
///////////////////////////////////////////////////////////////////////////////////
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { outFile.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
答案 0 :(得分:1)
在搜索和调试了一天半后,我找到了这个解决方案: 媒体商店没有访问应用程序特定的外部目录的权限,为了使媒体商店可以访问它们,您应该将文件复制到媒体目录,如下所示:
private void copyFiles() {
File directory = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC).toString());
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
String videoFileName = files[i].getName();
ContentValues valuesaudios;
valuesaudios = new ContentValues();
valuesaudios.put(MediaStore.Audio.Media.RELATIVE_PATH, "Music/" + "Folder");
valuesaudios.put(MediaStore.Audio.Media.TITLE, videoFileName);
valuesaudios.put(MediaStore.Audio.Media.DISPLAY_NAME, videoFileName);
valuesaudios.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
valuesaudios.put(MediaStore.Audio.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
valuesaudios.put(MediaStore.Audio.Media.DATE_TAKEN, System.currentTimeMillis());
valuesaudios.put(MediaStore.Audio.Media.IS_PENDING, 1);
ContentResolver resolver = getContentResolver();
Uri collection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri uriSavedAudio = resolver.insert(collection, valuesaudios);
ParcelFileDescriptor pfd;
try {
pfd = getContentResolver().openFileDescriptor(uriSavedAudio, "w");
FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
File storageDir = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC).toString());
File imageFile = new File(storageDir, files[i].getName());
FileInputStream in = new FileInputStream(imageFile);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
pfd.close();
} catch (Exception e) {
e.printStackTrace();
}
valuesaudios.clear();
valuesaudios.put(MediaStore.Audio.Media.IS_PENDING, 0);
getContentResolver().update(uriSavedAudio, valuesaudios, null, null);
Toast.makeText(this, files[i].getName(), Toast.LENGTH_SHORT).show();
}
}