繁体   English   中英

如何在Android 11和12中获取PDF文件路径

[英]How to get PDF File Path In Android 11 and 12

我尝试了很多代码来获取 android 11 或 12 中的 pdf 路径,但仅适用于 android 10 或以下设备。 你能帮我么? 我分享我的行代码

像这样的意图调用

Intent intent = new Intent();
            intent.setType("application/pdf");
            statusAdapter = "pdf";
            pos = position;
            intent.setAction(Intent.ACTION_GET_CONTENT);
            someActivityResultLauncher.launch(Intent.createChooser(intent, "Select PDF"));

someActivityResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        // There are no request codes
                        Intent data = result.getData();
                        if (data == null) {
                            //error
                            return;
                        }
                        try {
                            final Uri pdfUri= data.getData();
                            File pdfFile = new File(getPath(pdfUri));
                            long length = pdfFile.length();
                            length = length / 1024;
                            Toast.makeText(CreateSubEventActivity.this, "File Path : " + pdfFile.getPath() + ", File size : " + length + " KB", Toast.LENGTH_SHORT).show();
//                            uploadFile(imageFile);
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(CreateSubEventActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
                        }
                    }
                });

像这样调用getPath

public String getPath(Uri uri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor == null) return null;
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String s = cursor.getString(column_index);
        cursor.close();
        return s;
    }

如果您想访问一个文件或想要一个从 MediaStore 返回的 Uri 的文件路径,我有一个可以处理您可能遇到的所有异常。 这包括磁盘、内部和可移动磁盘上的所有文件。 例如,当从 Dropbox 选择一个文件时,该文件将被复制到您具有完全访问权限的应用程序目录,然后返回复制的文件路径。

在阅读完所有内容后,让我分享我修复这些东西的经验。

从 URI 获取输入 stream

final Uri pdfUri= data.getData();
getContentResolver().openInputStream(pdfUri)

然后用 InputStream 做你的事情,就像我使用 okHttp 上传 pdf

try {

RequestBody pdffile = new RequestBody() {
    @Override public MediaType contentType() { return MediaType.parse("application/pdf"); }
    @Override public void writeTo(BufferedSink sink) throws IOException {
        Source source = null;
        try {
            source = Okio.source(inputStream);
            sink.writeAll(source);
        } finally {
            Util.closeQuietly(source);
        }
    }
    @Override
    public long contentLength() {
        try {
            return inputStream.available();
        } catch (IOException e) {
            return 0;
        }
    }
};

RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
        .addFormDataPart("file", "fname.pdf", pdffile)
        //.addFormDataPart("Documents", value) // uncomment if you want to send Json along with file
        .build();

Request request = new Request.Builder()
        .url(serverURL)
        .post(requestBody)
        .build();

OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).writeTimeout(180, TimeUnit.SECONDS).readTimeout(180, TimeUnit.SECONDS)
        .addInterceptor(chain -> {
            Request original = chain.request();
            Request.Builder builder = original.newBuilder().method(original.method(), original.body());
            builder.header("key", key);
            return chain.proceed(builder.build());
        })
        .build();

client.newCall(request).enqueue(new Callback() {

    @Override
    public void onFailure(final Call call, final IOException e) {
        // Handle the error
        setIsLoading(false);
        getNavigator().uploadIssue("Facing some issue to upload this file.");
    }

    @Override
    public void onResponse(final Call call, final Response response) throws IOException {
        setIsLoading(false);
        if (!response.isSuccessful()) {
            getNavigator().uploadIssue("Facing some issue to upload this file.");

        }else {
            // Upload successful
            getNavigator().uploadedSucessfully();
        }

    }
});

return true;
} catch (Exception ex) {
    // Handle the error
    ex.printStackTrace();
}

这对我的情况有帮助 Android 11 希望任何人都能得到帮助

    private String copyFile(Uri uri, String newDirName) {
    Uri returnUri = uri;

    Cursor returnCursor = this.getContentResolver().query(returnUri, new String[]{
            OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE
    }, null, null, null);

    /*
     * Get the column indexes of the data in the Cursor,
     *     * move to the first row in the Cursor, get the data,
     *     * and display it.
     * */
    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
    returnCursor.moveToFirst();
    String name = (returnCursor.getString(nameIndex));
    String size = (Long.toString(returnCursor.getLong(sizeIndex)));

    File output;
    if (!newDirName.equals("")) {
        File dir = new File(this.getFilesDir() + "/" + newDirName);
        if (!dir.exists()) {
            dir.mkdir();
        }
        output = new File(this.getFilesDir() + "/" + newDirName + "/" + name);
    } else {
        output = new File(this.getFilesDir() + "/" + name);
    }
    try {
        InputStream inputStream = this.getContentResolver().openInputStream(uri);
        FileOutputStream outputStream = new FileOutputStream(output);
        int read = 0;
        int bufferSize = 1024;
        final byte[] buffers = new byte[bufferSize];
        while ((read = inputStream.read(buffers)) != -1) {
            outputStream.write(buffers, 0, read);
        }

        inputStream.close();
        outputStream.close();

    } catch (Exception e) {

        Log.e("Exception", e.getMessage());
    }

    return output.getPath();
}

String newPath = copyFileToInternalStorage(uri, getResources().getString(R.string.app_name));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM