【问题标题】:File not found error after selecting a file in android在android中选择文件后找不到文件错误
【发布时间】:2015-07-16 11:49:52
【问题描述】:

我想在我的 android 应用程序中打开一个 .pdf 文件。现在我可以浏览该 pdf 文件,并且在浏览该文件后我得到 File Not Found 错误时我检查文件是否存在。现在选择文件后,我选择的文件 Uri data.getData() 就像

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

当我使用data.getData().getPath().toString() 解析时的路径就像

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

// To Browse the file

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

选择文件后

//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) {
    }
}

【问题讨论】:

  • 如果文件存储在应用程序的内部缓存中,android 不允许其他应用程序访问它。所以也许这可能是问题所在。
  • 文件存放在外部目录中。
  • 您不能只将 uri 转换为绝对路径。你需要使用ContentResolver
  • 检查这个答案。 stackoverflow.com/a/16511111/891373
  • @Froyo 这也不适合我。我收到一个空指针异常“ava.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on an null object reference”

标签: java android pdf android-intent filenotfoundexception


【解决方案1】:
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        File pdfFile = new File(Environment.getExternalStorageDirectory(), "Case Study.pdf");

        try {
            if (pdfFile.exists()) {
                Uri path = Uri.fromFile(pdfFile);
                Intent objIntent = new Intent(Intent.ACTION_VIEW);
                objIntent.setDataAndType(path, "application/pdf");
                objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(objIntent);
            } else {
                Toast.makeText(MainActivity.this, "File NotFound", Toast.LENGTH_SHORT).show();
            }
        } catch (ActivityNotFoundException e) {
            Toast.makeText(MainActivity.this, "No Viewer Application Found", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

【讨论】:

  • 我的 .pdf 文件可能会更改。用户可以从自己的设备中选择文件。
【解决方案2】:

这不是答案,而是一种解决方法。

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();
}

将此文件的绝对路径传递给您的活动。

【讨论】:

  • 这也行不通。我们收到 Nullpointer 错误。我认为是因为 url 的问题。当我们从 url 检索路径时,文件不存在错误正在获取。
  • 你在哪里得到错误?用日志更新问题
  • 嘿抱歉.. 那是我的错误,我没有更改 some_temp_path。现在它工作正常。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
相关资源
最近更新 更多