DownloadManager从资产文件夹中下载文件? (Android Studio)

时间:2019-05-03 20:02:32

标签: java android

我正在制作一个音板应用程序,我希望用户能够从资产下载文件,以便他们可以将其设置为通知声音/铃声。我收到一条错误消息,说我只能从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);

2 个答案:

答案 0 :(得分:1)

使用AssetManagerits 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();