opening a file keeps failing in android file chooser

折月煮酒 提交于 2020-04-21 03:44:32

问题


I am trying to open and read file that was been saved either on the sdCard or on the google drive or on the internal memory by using the android file chooser ...

after I successfully chosen the file and I get the file path as shown on the following code when I try to read it it keeps throwing me the that the file does not exist / no such file or directory ....

my code:

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

                }
        }
    }
}

this is the logs :

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

I edited some of my code but still getting this

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)

for this :

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

        }
    }
}

回答1:


Open an InputStream for the choosen uri content scheme in the rigth way:

InputStream is = getContentResolver().openInputStream(data.getData());


来源:https://stackoverflow.com/questions/40317352/opening-a-file-keeps-failing-in-android-file-chooser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!