【问题标题】:Download a database file to the app directory下载一个数据库文件到app目录
【发布时间】:2017-02-16 02:01:53
【问题描述】:

我想在使用 DownloadManager 单击按钮时更新应用程序的 sqlite 数据库。

但它说“java.lang.IllegalArgumentException:不是文件 URI:/data/user/0/com.example.laudien.listviewtesting/databases/Employees”

我做错了什么?我需要获得许可吗?互联网权限已在清单中。

这是我的 updateDb() 方法的代码:

private void updateDb() {
    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); // create download manager
    DownloadFinishedReceiver receiver = new DownloadFinishedReceiver(); // create broadcast receiver
    registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); // register the receiver
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DATABASE_URL)); // create a download request

    // delete database file if it exists
    File databaseFile = new File(getDatabasePath(DATABASE_NAME).getAbsolutePath());
    if(databaseFile.exists())
        databaseFile.delete();

    request//.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN) // not visible
        .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI) // only via wifi
        .setDestinationUri(Uri.parse("file:" + getDatabasePath(DATABASE_NAME).getAbsolutePath())); // set path in app dir
    downloadManager.enqueue(request); // enqueue the download request
}

【问题讨论】:

  • 嘿!为什么不直接重用 databaseFile URI 呢?即databaseFile.toURI();进入 setDestinationUri() 方法。此外,如果您遇到权限问题,您会收到 SecurityException。我很快就适应并尝试了你的例子来下载谷歌的标志,它工作得很好。例如.setDestinationUri(Uri.parse("file:" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/google.png"))
  • 感谢您的回复!我试过 .setDestinationUri(databaseFile.toURI()) 但它说“android.net.Uri”不能应用于“java.net.Uri”。然后我尝试了 .setDestinationUri(Uri.fromFile(databaseFile)) 但它显示“java.lang.SecurityException: Unsupported path /data/user/0/com.example.laudien.listviewtesting/databases/Employees” 然后我添加了 WRITE_EXTERNAL_STORAGE 和READ_EXTERNAL_STORAGE 权限并在模拟器中接受它,但它是同样的错误。

标签: java android sqlite


【解决方案1】:

您的路径/data/user/0/com.example.laudien.listviewtesting 是您应用的私有内部存储器。您使用getFilesDir() 获得它。其他应用程序无权访问。包括下载管理器。请改用getExternalStorageDirectory() 提供的外部存储器。

【讨论】:

    【解决方案2】:

    对于想要将敏感文件下载到应用程序的私人文件夹的任何人,我不得不说下载管理器无法访问内部存储中的这些文件夹。 但解决方案是在广播接收器中移动下载的文件。 例如将这样的行放在BroadcastReceiver中:

    String destinationFileName = "test.mp4";
    String filePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
    copyFile(Uri.parse(filePath).getPath(), context.getFilesDir() + "/" + destinationFileName);
    

    而 copyFile 是这样的方法:

    private void copyFile(String inFileName, String outFileName) throws IOException {
    
        InputStream myInput = new FileInputStream(inFileName);
    
        OutputStream myOutput = new FileOutputStream(outFileName);
    
        // Transfer bytes from the input file to the output file
        byte[] myBuffer = new byte[1024];
        int length;
        while ((length = myInput.read(myBuffer)) > 0)
            myOutput.write(myBuffer, 0, length);
    
        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }
    

    【讨论】:

    • 什么是cursor?
    • @Starwave 我使用游标从下载结果中读取特定的 COLUMN_* 常量 => Cursor cursor = downloadManager.query(new DownloadManager.Query());
    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多