【问题标题】:I am trying to upload the downloaded pdf file in Android 11我正在尝试在 Android 11 中上传下载的 pdf 文件
【发布时间】:2022-09-28 15:26:13
【问题描述】:

我正在尝试在 Android 11 中上传下载的 pdf 文件。在那,我正在尝试获取 pdf 文件的路径,光标移动到 next() 方法返回错误的 android 11。

下载的 pdf 文件为我们提供了 Uri,内容://com.android.providers.media.documents/document/pdffilename:33A

我在尝试访问此 URI 的路径时遇到问题,我需要有关此 URI 的帮助。

  • 没有路径,因为它不必指向本地文件系统上可供您访问的文件。直接使用Uri。例如,如果您打算使用 OkHttp 或 Retrofit 来上传 PDF 内容,您可以使用 InputStreamRequestBody 使用 Uristackoverflow.com/a/56308643/115145

标签: android pdf media document


【解决方案1】:
  call handleUri() function from your Activity it will return file path and will solve your issue. 
    1. java version :-  
        public static String handleUri(
        final Context context,Uri uri)
        {
        String type = "";
        if(context.getContentResolver().getType(uri)
        .equalsIgnoreCase("application/pdf")) 
        {type = ".pdf";}
        if(context.getContentResolver().getType(uri)
        .equalsIgnoreCase("application/vnd.openxmlformats- officedocument.wordprocessingml.document")) {
                type = ".docx";
            }
    
            File dir = new File(context.getCacheDir(), "hrm");
            dir.mkdir();
            File file = new File(dir, System.currentTimeMillis() 
        + "" + type);
            try {
                
     
     
     
     copyStreamToFile(context.getContentResolver().openInputStream 
       (uri), file);
         } catch (FileNotFoundException e) {
         e.printStackTrace();  }
        return file.getAbsolutePath();
        }
        private static void copyStreamToFile(InputStream 
        inputStream, File outputFile) {
        try { 
        OutputStream outputStream = new 
        FileOutputStream(outputFile);
        byte[] buffer = new byte[1024];
        while (true) {
        int count = inputStream.read(buffer);
        if (count > 0) {
        outputStream.write(buffer, 0, count);
        }
         else break;
        outputStream.flush();
        }}
        catch (FileNotFoundException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }}  

2. kotlin version:-  

   

    fun handleUri(uri: Uri): String? {
       context.apply {
            val type = when (contentResolver.getType(uri)) {
                "application/pdf" -> ".pdf"
                //another types
                else -> return null
            }
            val dir = File(cacheDir, "dir_name").apply { mkdir() }
            val file = File(dir, "${System.currentTimeMillis()}$type")
            copyStreamToFile(
                contentResolver.openInputStream(uri)!!,
                file
            )
            if (file.length() / 1024 < 2.0) {
                return file.absolutePath
            } else {
                showToast("Maximum 2 MB file size allow pdf.")
                return null
            }
        }
    
    }
    
    private fun copyStreamToFile(inputStream: InputStream, outputFile: File) {
        inputStream.use { input ->
            val outputStream = FileOutputStream(outputFile)
            outputStream.use { output ->
                val buffer = ByteArray(4 * 1024) // buffer size
                while (true) {
                    val byteCount = input.read(buffer)
                    if (byteCount < 0) break
                    output.write(buffer, 0, byteCount)
                }
                output.flush()
            }
        }
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2023-01-19
    • 2021-08-23
    • 1970-01-01
    • 2021-05-28
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多