簡體   English   中英

Android Studio - 將文件直接存儲或復制到 SD 卡或手機的 ROOT

[英]Android Studio - Store or Copy a File direct to ROOT of SD Card or Phone

Android Studio - 將文件直接存儲或復制到 SD 卡或手機的 ROOT

如何將文件從應用程序目錄復制到我的 SD 卡或手機的根目錄? 使用我在 inte.net 上找到的代碼,我只能在以下目錄中復制或創建:“/storage/emulated/0/Android/data/com.example.filme3/files”但我想存儲在“/storage/ emulated/0" 或者在我的外部 SD 卡上。 “存儲/9C33-6BBD”

Context context = getApplicationContext();
File path = context.getExternalFilesDir(null);
copyFile(new File(path, "filme1_alle.txt"), new File( path,"testfile.txt"));

public static boolean copyFile(File source, File dest){
        try{
            // Declaration et ouverture des flux
            java.io.FileInputStream sourceFile = new java.io.FileInputStream(source);

            try{
                java.io.FileOutputStream destinationFile = null;

                try{
                    destinationFile = new FileOutputStream(dest);

                    // Lecture par segment de 0.5Mo
                    byte buffer[] = new byte[512 * 1024];
                    int nbLecture;

                    while ((nbLecture = sourceFile.read(buffer)) != -1){
                        destinationFile.write(buffer, 0, nbLecture);
                    }
                } finally {
                    destinationFile.close();
                }
            } finally {
                sourceFile.close();
            }
        } catch (IOException e){
            e.printStackTrace();
            return false; // Erreur
        }

        return true; // Rsultat OK
    }

自 Android KitKat 以來,可移動微型 SD 卡是只讀的。

只有使用存儲訪問框架,您才能寫入它們。

在 Adroid 11+ 上,外部存儲的根目錄不可寫。 然后使用公共目錄。

查看 MANAGE_EXTERNAL_STORAGE 權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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