簡體   English   中英

將視頻文件存儲在Android 5中的SD卡上

[英]Store video files on SD card in Android 5

我正在借助android.media.MediaRecorder類錄制視頻,該類接受輸出文件的路徑字符串( MediaRecorder.setOutputFile(String) ),盡管該方法有一個版本可以接受FileDescriptor

我需要存儲巨大的視頻文件,因此我想使用SD卡。 為了獲取相關目錄的路徑,我使用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) 事實證明,生成的路徑用於“模擬”存儲( /sdcard/… ),而不是實際的SD卡(在我的Xperia Z3 Compact,Android 5.1.1上為/sdcard1/ )。

我試圖對/sdcard1/ (以及/storage/sdcard1 )進行硬編碼,但是卻出現了IOException討論了拒絕權限。 當然,我有

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我聽說過4.4(又名Storage Access Framework)之后對SD卡的訪問發生了巨大變化,但是找不到在這種情況下如何完成工作的簡單明了的解釋。 在簡短的解決方案上有什么幫助嗎?

具有硬編碼路徑的PS解決方案對我來說還可以,因為我將僅在手機上使用此應用程序。

因此,我不得不全面討論上述存儲訪問框架

步驟1:要求建立檔案

注意:下面的fileName實際上是文件本身的名稱,沒有完整(所需)的路徑。 例如myvideo.mp4

private void createFile(String fileName) {
    String mimeType = "video/mp4";
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

    intent.addCategory(Intent.CATEGORY_OPENABLE);

    intent.setType(mimeType);
    intent.putExtra(Intent.EXTRA_TITLE, fileName);
    startActivityForResult(intent, WRITE_REQUEST_CODE /* is some random int constant*/);
}

步驟2:回調以處理目錄選擇

調用createFile (參見步驟1)后,將為用戶提供一個系統對話框,用於選擇要在其中存儲文件的目錄。 他們做出選擇后,將使用新文件的所需URI觸發處理程序。 我們可以將其轉換為FileDescriptor並使用MediaRecorder.setOutputFile相應版本。

@Override
public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {

    if (requestCode == WRITE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        if (resultData != null) {
            URI outputFileUri = resultData.getData();
            FileDesriptor outputFileDescriptor = getContentResolver().openFileDescriptor(outputFileUri, "w").getFileDescriptor()
            mMediaRecorder.setOutputFile(outputFileDescriptor);
            mMediaRecorder.start();
        }
    }
}

此方法應在一些 Activity類中實現。

有待改進

因此,用戶每次啟動新視頻時都必須在系統菜單中單擊Save 當我的應用程序可以自行決定目錄時,恢復KitKat之前的狀態會很好。 考慮到我使用“專業”應用程序的經驗,這確實是可能的。 例如,“ ES Explorer”要求用戶僅對SD卡進行一次明確的操作許可(使用類似的系統對話框)。

此代碼應解決您的問題:

 public String createVideoFilePath() {
        String time = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File[] pathsArray = getExternalFilesDirs(Environment.DIRECTORY_DCIM);

/ 通常,pathArray [1]包含可移動SD卡上必要文件夾的路徑。 因此,我們通常可以只使用pathArray [1]而不是使用for語句 / 在pathArray中搜索必要的文件

        for (File f : pathsArray) {
            if ((f != null) && (Environment.isExternalStorageRemovable(f))) {
                return f.getPath() + File.separator +"video_"+ time + ".mp4";
            }
        }

        return pathsArray[0].getPath() + File.separator +"video_"+ time + ".mp4";

    }

如果不存在可移動SD,則此方法返回文件在可移動SD卡上或仿真存儲中的位置

只需將此方法用作MediaRecorder.setOutputFile(String) 更新的參數MediaRecorder.setOutputFile(String) 非常重要的一點是,位於文件夾中的所有文件都是通過Context. getExternalFilesDirs(String typr) 卸載應用程序后,將刪除Context. getExternalFilesDirs(String typr)

暫無
暫無

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

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