[英]DownloadManager a file from assets folder? (Android Studio)
我正在制作一個音板應用程序,希望用戶能夠從資產下載文件,以便他們可以將其設置為通知聲音/鈴聲。 我收到一條錯誤消息,說我只能從HTTP / HTTPS下載文件。 有沒有解決的辦法?
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("content://com.thingy.app/" + filename));
request.setDescription("Soundbite from ");
request.setTitle(filename);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
使用AssetManager
及其open()
方法獲取資產內容上的InputStream
。 然后,將字節復制到要創建的文件上的FileOutputStream
。
我使用它來將SQLite數據庫從Assets文件夾復制到內部存儲中:
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_IN);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.