这个问题在这里已经有答案了:
android kotlin:获取一个filenotfoundexception,其文件名是从文件选择器中选择的(3个答案)
android-获取从文件资源管理器中选择的.txt文件的实际路径(1个答案)
8天前关门了。
我试图从explorer(移动存储)获取文件。不幸的是,我面临一个问题。但是,当我早些时候尝试的时候,它是正确的工作。也许它在新的android工作室里不起作用。如果可能添加您的源代码(文件上传)来帮助我。
价值观:
String mediaPath, mediaPath1;
String[] mediaColumns = {MediaStore.Video.Media._ID};
按钮:
btnFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), 3);
}
});
活动结果:
//try {
// When an file is picked
if (requestCode == 3 && resultCode == RESULT_OK && null != data) {
// Get the file from data
String path = data.getStringExtra(mediaPath);
File file = new File(path);
Uri selectedFile = Uri.fromFile(new File(file.getAbsolutePath()));
String[] filePathColumn = {MediaStore.Files.FileColumns.MEDIA_TYPE};
Cursor cursor = getContentResolver().query(selectedFile, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mediaPath = cursor.getString(columnIndex);
txt.setText(path);
cursor.close();
// try {
// String parsedText="";
// PdfReader reader = new PdfReader(mediaPath);
// int n = reader.getNumberOfPages();
// for (int i = 0; i <n ; i++) {
// parsedText = parsedText+ PdfTextExtractor.getTextFromPage(reader, i+1).trim()+"\n"; //Extracting the content from the different pages
// }
// Log.d("PDF_TEXT",parsedText);
// System.out.println(parsedText);
// reader.close();
// } catch (Exception e) {
// System.out.println(e);
// }
}
else {
Toast.makeText(this, "You haven't picked any file", Toast.LENGTH_LONG).show();
}
// } catch (Exception e) {
// Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
// Log.d("EXCEPTION_ERROR",e.toString());
// }
问题:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)
at com.example.pdfreader.MainActivity.onActivityResult(MainActivity.java:68)
``` `MainActivity.java:68` 代表 `String path = data.getStringExtra(mediaPath);`
暂无答案!
目前还没有任何答案,快来回答吧!