As the title says i'm saving some files - mostly audio - in my app specified External directory (android/data/com.xxx.xxx), now i want to access to those audio files with Mediastore but i can't how should i do this? is it possible for media store to access this kind of private directory?
it worth mentioning that the whole data directory has a .nomedia file
UPDATE :
files are saved in .../android/data/com.xxx.xxx/files/music were getExternalFilesDir
stores files
mediastore finds other audio files but it can't access to my files in that directory
context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
BASE_PROJECTION, selection, selectionValues, sortOrder);
UPDATE2: used following scan after creating files
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);
}
}
OK after searching and debugging for a day and half i found this solution: Media Store doesn't have access to app specific external directory and in order to media store have access to them you should copy your files to a media directory as below :
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.