在onActivityResult回调中,如何从从PDF选择器意图库获得的pdfUri获取文件路径? [英] How to get File path from pdfUri obtained from PDF chooser intent library, in onActivityResult call back?

查看:118
本文介绍了在onActivityResult回调中,如何从从PDF选择器意图库获得的pdfUri获取文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考同一类别中提出的问题,并对此进行扩展:

With reference to the question asked in the same category, and as an extension of this:

如何从指向PDF文档的URI获取文件路径?

(How to get the file path from a URI that points to a PDF document?)

我的问题:

在我的HTC 816 Desire手机中[注:此设备已升级到MarshMallow,但仍然崩溃了KitKat.作为开发人员,我经历过Camera故意打开以获取其捕获的图像,但从未释放其资源.当我尝试拍摄第二张照片时,这导致内部崩溃.],返回的路径以"/document/primary:"开头,并且保留在'pdfUri.getPath()'中,导致找不到文件异常.另外,当我在'MediaStore.Images.Media.DATA'中搜索时,返回的列索引为-1.

In my HTC 816 Desire phone [Note: this device is upgraded to MarshMallow but still has crashes of KitKat. As a developer, I have experienced that Camera opened by intent to get the image captured by it, never releases its resources. This gives me an internal crash, when I try to take the second picture.], the returned path is starting with "/document/primary:", and it is retained in 'pdfUri.getPath()' resulting in file not found exception. Also when I search in 'MediaStore.Images.Media.DATA', column index returned is -1.

Q1.当列索引为-1时,我是否应该从文件路径中简单删除"/document/primary:".因为在更高版本的手机(23及更高版本)中,此(pdfUri.getPath())可以正常工作,为我提供了正确的路径.

Q1. Should I simply remove "/document/primary:" from the file path, when the column index comes -1. As because in higher phones (23 and above), this (pdfUri.getPath()) works OK, giving me correct path.

Q2.我应该从哪里获得手机的补丁程序来修复Camera resource not releasing错误,以及固件级别的其他错误.

Q2. Where should I get the patches for my phone to fix Camera resource not releasing bug, and there are other bugs at Firm-Ware level.

如果没有被正确问到,也请通过正确地问这个问题来指导我.

Please guide me, as well, by asking this question correctly, if in case it is not been asked accurately.

推荐答案

为了解决该问题,我尝试执行以下操作:

In order to resolve the problem, I am trying to do the following:

  1. 我将使解决方案获得的通用代码适用于PDF以外的许多其他文件格式:

  1. I am going to make the code generic, that I got as a solution, for many other file formats other than PDF:

private String saveFileFromUri(Uri pdfUri){
    try{
        InputStream is=
        baseActivity.getContentResolver().openInputStream(pdfUri);
        byte[]bytesArray=new byte[is.available()];
        int read=is.read(bytesArray);
    //write to sdcard

    File dir=new File(Environment.getExternalStorageDirectory(),"/PRJ");
    boolean mkdirs=dir.mkdirs();
    File myPdf=new 
    File(Environment.getExternalStorageDirectory(),"/PRJ/myPdf.pdf");
    if(read==-1&&mkdirs){

    }
    FileOutputStream fos=new FileOutputStream(myPdf.getPath());
    fos.write(bytesArray);
    fos.close();
    //            System.out.println(fileString);
    return myPdf.getPath();
}catch(FileNotFoundException e){
    e.printStackTrace();
}catch(IOException e){
    e.printStackTrace();
}
return null;}

  • 为了能够释放Camera资源,我将使用基于表面视图的Camera Activity,它可以做到这一点.这是代码的一部分:

  • In order to be able to release Camera resource, I will use surface view based Camera Activity which allows to do that. Here is a part of the code:

    释放相机资源:

        @Override
    public void onPause() 
    {
        super.onPause();
    
            if (mCamera != null){
                //              mCamera.setPreviewCallback(null);
                mPreview.getHolder().removeCallback(mPreview);
                releaseMediaRecorder();
                mCamera.release();        // release the camera for other applications
                mCamera = null;
    
            }
        }
    
        @Override
        public void onResume() {
            super.onResume();
            if (mCamera == null) {
                mCamera=getCameraInstance();
                mPreview = new CameraPreview(this.getActivity(), mCamera);
                preview.addView(mPreview);
            }
        }
    

    快乐编码:-)

    这篇关于在onActivityResult回调中,如何从从PDF选择器意图库获得的pdfUri获取文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆