简体   繁体   English

有没有办法通过媒体存储访问特定于应用程序的外部存储文件夹?

[英]Is there a way to access app-specific external storage folder with media store?

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?正如标题所说,我正在将一些文件(主要是音频)保存在我的应用程序指定的外部目录 (android/data/com.xxx.xxx) 中,现在我想使用 Mediastore 访问这些音频文件,但我不知道该怎么做我这样做? 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值得一提的是,整个数据目录都有一个 .nomedia 文件

UPDATE :更新 :

files are saved in .../android/data/com.xxx.xxx/files/music were getExternalFilesDir stores files文件保存在 .../android/data/com.xxx.xxx/files/music 中getExternalFilesDir存储文件

mediastore finds other audio files but it can't access to my files in that directory mediastore 找到其他音频文件,但无法访问该目录中的我的文件

context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                BASE_PROJECTION, selection, selectionValues, sortOrder);

UPDATE2: used following scan after creating files 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);
    }
}

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();
    }



}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 13 在外部存储中创建应用程序特定文件夹 - Android 13 create a app specific folder in External Storage 如何在 android 中拍摄图像并将其保存在图库以及特定于应用程序的存储中? (java,android工作室) - How to take images in android and save it on gallery as well as in the app-specific storage ? (java, android studio) 将资产文件夹子目录复制到外部存储中的特定文件夹 - Copying assets folder subdirectories to specific folder in external storage 无法在Android中搜索应用程序特定文件的原因和解决方案 - Reason for not being able to search the app-specific files in Android and solution 如何将文件从Uri复制到外部数据存储中的特定文件夹 - how to copy file from Uri to a specific folder in external data storage 在外部存储上创建文件夹 - Creating Folder on External Storage 如何在外部应用程序 android 中打开存储在应用程序特定存储中的文件? - How open file stored in app specific storage in external app android? 如何使用 Scoped Storage 访问特定文件夹并将文件写入其中 - How to gain access to a specific folder and write a file into it using Scoped Storage 不能访问其他应用程序创建的外部存储中的文件 - Not abe to access file in External Storage created by other app 有没有一种方法可以在Android上的“我的文件”(外部存储)中创建文件夹并将文件写入此文件夹? - Is there a way to create a folder within My Files on Android (external storage) and write files to this folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM