Android 10(API-29)文件编写

时间:2019-09-22 13:55:22

标签: android file permissions android-permissions

由于https://material-ui-pickers.dev/api/DatePicker无法在Android Q +中使用,因此任何人都可以提供有关如何在公开发布的Android Q上保存文件的准确教程。

几天来我一直在处理此问题,无法解决任何问题。

我正在尝试下载一些CSS,JS和HTML文件,以将其加载到Webview中。

现在,我有以下用Kotlin编写的代码:

<DatePicker
    okLabel="Text"
    clearLabel="Text"
    cancelLabel="Text"
    ...
/>

和DownloadFileAsyncTask:

companion object {
    private const val TAG = "MainActivity"
    private var rootDir: Path? = null
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    ...

    rootDir = getExternalFilesDir(null)
    Log.v(TAG, "RootDir: ${rootDir!!.path}")

    val cacheDir = File(rootDir!!, "cache")
    if(!cacheDir.exists()) {
         Log.v(TAG, "Creating cache dir (${cacheDir.path})")
         try {
            if (cacheDir.mkdirs())
                Log.v(TAG, "Created cache dir correctly")
            else
                Log.v(TAG, "Could not create cache dir")
        }catch (ex: IOException){
            Log.e(TAG, "Could not create cache dir: ", ex)
        }catch (ex: SecurityException) {
            Log.e(TAG, "Not allowed to create cache dir: ", ex)
        }
    }

    val cssDir = File(cacheDir, "css")
    if(!cssDir.exists()){
        Log.v(TAG, "Creating css dir (${cssDir.path})")
        try {
            if (cacheDir.mkdirs())
                Log.v(TAG, "Created css dir correctly")
            else
                Log.v(TAG, "Could not create css dir")
        }catch (ex: IOException){
            Log.e(TAG, "Could not create css dir: ", ex)
        }catch (ex: SecurityException) {
            Log.e(TAG, "Not allowed to create css dir: ", ex)
        }
    }

    val siteGlobal = File(cssDir, "site_global.css")
    if(!siteGlobal.exists())
        DownloadFileAsyncTask().execute(DownloadFileAsyncTask.DownloadArgs("...", siteGlobal.path))
}

我对Logcat的回答是:

    private class DownloadFileAsyncTask :
        AsyncTask<DownloadFileAsyncTask.DownloadArgs, Int, Void?>() {

        data class DownloadArgs(val url : String, val savePath : String)

        override fun doInBackground(vararg downloadFiles: DownloadArgs?): Void? {
            for(downloadFile in downloadFiles) {
                val urlStr = downloadFile!!.url
                val outputFilePath = downloadFile.savePath
                val outputFile = File(outputFilePath)
                val url = URL(urlStr)

                Log.v(TAG, "Connecting...")
                val connection = url.openConnection() as HttpURLConnection
                connection.connect()

                if (connection.responseCode != HttpURLConnection.HTTP_OK) {
                    throw InterruptedException("Server returned HTTP ${connection.responseCode} ${connection.responseMessage}")
                }

                Log.v(TAG, "Connection correct")
                if (!outputFile.exists()) {
                    Log.v(TAG, "Creating ${outputFile.path}")
                    outputFile.createNewFile()
                }

                val fileLength = connection.contentLength

                val input = connection.inputStream
                val output = FileOutputStream(outputFile)

                val data = ByteArray(4096)
                var total: Long = 0
                var count: Int
                Log.v(TAG, "Starting download...")
                while (true) {
                    count = input.read(data)
                    if (count < 0) break
                    total += count
                    if (fileLength > 0)
                        publishProgress((total * 10 / fileLength).toInt())
                    output.write(data, 0, count)
                }

                output.close()
                input?.close()
                connection.disconnect()
            }

            return null
        }
    }

显然,由于目录不存在,它在AsyncTask中的V/MainActivity: RootDir: /storage/emulated/0/Android/data/.../files V/MainActivity: Creating cache dir (/storage/emulated/0/Android/data/.../files/cache) V/MainActivity: Created cache dir correctly V/MainActivity: Creating css dir (/storage/emulated/0/Android/data/.../files/cache/css) V/MainActivity: Could not create css dir V/MainActivity: Connecting... V/MainActivity: Connection correct V/MainActivity: Creating /storage/emulated/0/Android/data/.../files/cache/css/site_global.css 处以java.io.IOException: No such file or directory崩溃了。

由于未显示有关创建文件夹的错误,因此它仅表明无法创建目录,因此我不知道我在做什么错。

我已经添加并授予了outputFile.createNewFile(),以解决问题,而它并没有解决任何问题。

1 个答案:

答案 0 :(得分:4)

getExternalStorageDirectory()在API-29中不受欢迎

检查步骤

  1. 从网络服务器下载和上传文件“使用FTP连接。(这是一种方法,但不仅如此)

How to download a file from FTP server to Android device?

  1. 将其保存到外部存储设备
    //private static void createFile(Context ctx, String fileName, byte text)
    private static void createFile(Context ctx, String fileName, String text) {
        File filesDir = ctx.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        assert filesDir != null;
        if (!filesDir.exists()){
           if(filesDir.mkdirs()){
            }
        }
        File file = new File(filesDir, fileName);
        try {
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    throw new IOException("Cant able to create file");
                }
            }    
            OutputStream os = new FileOutputStream(file);
            byte[] data = text.getBytes();
            os.write(data);
            os.close();
            Log.e("TAG", "File Path= " + file.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }
  1. 使用加载URL将文件加载到webview中,然后像问题Android Web-View : Inject local Javascript file to Remote WebpageAndroid - How to inject custom css into external webpage in webview一样注入