我正在尝试使用android文件选择器打开并读取已保存在sdCard或google驱动器或内部内存中的文件...
在我成功选择文件后,当我尝试读取它时,我得到了如下代码所示的文件路径,它一直让我知道文件不存在/没有这样的文件或目录....
我的代码:
public class MainActivity extends AppCompatActivity {
private static final int CHOOSE_FILE_REQUEST_CODE = 1;
String[] ids;
TextView textView;
Button btnImport;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text_view);
btnImport = (Button) findViewById(R.id.btn_import);
btnImport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "File");
startActivityForResult(i, CHOOSE_FILE_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
return;
}
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String path = data.getData().getPath();
String url = data.getData().getPath();
File file = new File(url);
Log.i("***", path);
// InputStream inputStream = getResources().openRawResource(R.raw.sample);
InputStream inputStream = null ;
BufferedReader bufferedReader = null;
try {
inputStream = new FileInputStream(file);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String csvLine;
StringBuffer stringBuffer = new StringBuffer();
while ((csvLine = bufferedReader.readLine()) != null) {
ids = csvLine.split(",");
for (int i = 0; i < ids.length; i++) {
if (i % 19 == 1) {
stringBuffer.append(ids[i]);
stringBuffer.append("\n");
Log.i("***", stringBuffer.toString());
}
textView.setText(stringBuffer.toString());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
这是日志:
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: java.io.FileNotFoundException: /document/E896-1C04:Download/39196035.doc: open failed: ENOENT (No such file or directory)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at com.example.ndrzhr.readcsv02.MainActivity$override.onActivityResult(MainActivity.java:69)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at com.example.ndrzhr.readcsv02.MainActivity$override.access$dispatch(MainActivity.java)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at com.example.ndrzhr.readcsv02.MainActivity.onActivityResult(MainActivity.java:0)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:7137)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4916)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at android.app.ActivityThread.access$1600(ActivityThread.java:221)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at android.os.Looper.loop(Looper.java:158)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7224)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at libcore.io.Posix.open(Native Method)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: ... 15 more
10-29 11:05:48.130 14716-14716/com.example.ndrzhr.readcsv02 I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7501f3b time:713512721
10-29 11:15:48.520 14716-14716/com.example.ndrzhr.readcsv02 V/ActivityThread: updateVisibility : ActivityRecord{5091c6e token=android.os.BinderProxy@7501f3b {com.example.ndrzhr.readcsv02/com.example.ndrzhr.readcsv02.MainActivity}} show : true
我编辑了一些代码,但仍然得到了这个
10-29 12:43:27.360 19779-19779/com.example.ndrzhr.readcsv02 W/System.err: java.io.FileNotFoundException: content:/com.android.externalstorage.documents/document/primary%3ADownload%2FUntitled%20document.txt: open failed: ENOENT (No such file or directory)
为此:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String type = data.getType();
Log.i("***", "Pick completed: " + uri + " " + type);
if (uri != null) {
String path = uri.toString();
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
//inputStream = new FileInputStream(file);
fileReader = new FileReader(path);
bufferedReader = new BufferedReader(fileReader);
String csvLine;
StringBuffer stringBuffer = new StringBuffer();
while ((csvLine = bufferedReader.readLine()) != null) {
ids = csvLine.split(",");
for (int i = 0; i < ids.length; i++) {
if (i % 19 == 1) {
stringBuffer.append(ids[i]);
stringBuffer.append("\n");
Log.i("***", stringBuffer.toString());
textView.setText(stringBuffer.toString());
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
以严格的方式为选择的uri内容方案打开InputStream
:
InputStream is = getContentResolver().openInputStream(data.getData());