我正在制作一个音板应用程序,我希望用户能够从资产下载文件,以便他们可以将其设置为通知声音/铃声。我收到一条错误消息,说我只能从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);
答案 0 :(得分:1)
使用AssetManager
和its open()
method获取资产内容的InputStream
。然后,将字节复制到要创建的文件上的FileOutputStream
。
答案 1 :(得分:0)
我用它来将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();