簡體   English   中英

有沒有辦法通過媒體存儲訪問特定於應用程序的外部存儲文件夾?

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

正如標題所說,我正在將一些文件(主要是音頻)保存在我的應用程序指定的外部目錄 (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);
    }
}

好的,經過一天半的搜索和調試,我找到了這個解決方案:媒體商店無權訪問特定於應用程序的外部目錄,為了媒體商店有權訪問它們,您應該將文件復制到媒體目錄,如下所示:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM