找不到文件的错误选择在android系统文件后 [英] File not found error after selecting a file in android

查看:284
本文介绍了找不到文件的错误选择在android系统文件后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Andr​​oid app.now打开.pdf文件我可以浏览pdf文件和浏览文件后,我得到的文件未找到错误时,我检查文件存在与否。现在,选择文件后,我选择的文件乌里 data.getData()

内容://com.android.externalstorage.documents/document/6333-6131:SHIDHIN.pdf

和路径时,我分析采用 data.getData()的getPath()。的toString()

/document/6333-6131:SHIDHIN.pdf 这是我的code。请帮我。

  //浏览文件意向意图=新意图(Intent.ACTION_GET_CONTENT);
intent.setType(应用程序/ PDF格式);
startActivityForResult(意向,PICK_FILE_REQUEST);

选择文件后

  //的onActivityResult公共无效的onActivityResult(最终诠释请求code,INT结果code,意图数据){
    尝试{
        开关(要求code){
            案例PICK_FILE_REQUEST:
                如果(结果code == RESULT_OK){
                    尝试{
                        乌里了fileURI = data.getData();
                        。字符串路径= fileUri.getPath()的toString();
                        文件f =新的文件(路径);
                        如果(f.exists()){
                            的System.out.println(\\ n ****乌里:>+ fileUri.toString());
                            的System.out.println(\\ n ****路径:>+ path.toString());
                            最终意向意图=新意图(MainActivity.this,ViewPdf.class);
                            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,路径);
                            startActivity(意向);
                        }其他{
                            的System.out.println(\\ n ****文件不存在:>+路径);
                        }                    }赶上(例外五){
                        ShowDialog_Ok(错误,无法打开文件);
                    }
                }
                打破;
        }
    }赶上(例外五){
    }
}


解决方案

这是没有答案,但一个解决办法。

 文件fil​​e =新的文件(some_temp_path); #你也可以使用应用程序的内部缓存来存储文件
FOS的FileOutputStream =新的FileOutputStream(文件);。InputStream为= context.getContentResolver()openInputStream(URI);
字节[]缓冲区=新的字节[1024];
INT LEN = 0;
尝试{
    LEN = is.​​read(缓冲液);
    而(LEN!= -1){
        fos.write(缓冲液,0,LEN);
        LEN = is.​​read(缓冲液);
    }    fos.close();
}赶上(IOException异常五){
    e.printStackTrace();
}

通过这个文件的绝对路径到您的活动。

I want to open a .pdf file in my android app.now i can browse the pdf file and after browsing the file I am getting File Not Found Error when i check the file exist or not. Now after selecting the file my selected file Uri data.getData() is like

content://com.android.externalstorage.documents/document/6333-6131:SHIDHIN.pdf

and the path when i parse using data.getData().getPath().toString() is like

/document/6333-6131:SHIDHIN.pdf Here is my code. Please Help me.

// To Browse the file

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(intent, PICK_FILE_REQUEST);

After selecting file

//onActivityResult

public void onActivityResult(final int requestCode, int resultCode, Intent data) {
    try {
        switch (requestCode) {
            case PICK_FILE_REQUEST:
                if (resultCode == RESULT_OK) {
                    try {
                        Uri fileUri = data.getData();
                        String path  = fileUri.getPath().toString();
                        File f = new File(path);
                        if (f.exists()) {
                            System.out.println("\n**** Uri :> "+fileUri.toString());
                            System.out.println("\n**** Path :> "+path.toString());
                            final Intent intent = new Intent(MainActivity.this, ViewPdf.class);
                            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
                            startActivity(intent);
                        } else {
                            System.out.println("\n**** File Not Exist :> "+path);
                        }

                    } catch (Exception e) {
                        ShowDialog_Ok("Error", "Cannot Open File");
                    }
                }
                break;
        }
    } catch (Exception e) {
    }
}

解决方案

This is not the answer but a workaround.

File file = new File("some_temp_path"); # you can also use app's internal cache to store the file
FileOutputStream fos = new FileOutputStream(file);

InputStream is = context.getContentResolver().openInputStream(uri);
byte[] buffer = new byte[1024];
int len = 0;
try {
    len = is.read(buffer);
    while (len != -1) {
        fos.write(buffer, 0, len);
        len = is.read(buffer);
    }

    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}

pass this file's absolute path to your activity.

这篇关于找不到文件的错误选择在android系统文件后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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