openFileInput():IllegalArgumentException

时间:2018-01-13 14:32:02

标签: android fileinputstream illegalargumentexception

我正在尝试从我之前写过的设备中读取数据。但是当我尝试读取它时,我收到了IllegalArgumentException。实际上我试图写入外部SD卡但它已存储在设备存储中。我努力但无法找到将数据写入外部SD卡的解决方案。但真正的问题是我无法从文件中读取。虽然我已经在stackoverflow帖子中看到了很多这样的代码,但是之前将openFileInput分配给FileInputStream给了我一个NullPointerException。下面是代码。请帮我在代码中找到错误。

代码:

 public String getExternalSdCardPath()
    {
        String path=null;
        File sdcardfile=null;
        List<String> sdpossiblepath= Arrays.asList("external_sd","ext_sd","external","extSdCard","sdcard2");
        for (String sdpath:sdpossiblepath)
        {
            File file=new File("mnt/",sdpath);
            if (file.isDirectory()&&file.canWrite())
            {
                path=file.getAbsolutePath();
                String timestamp=new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
                File testWritable=new File(path,"test_"+timestamp);
                if (testWritable.mkdirs())
                {
                    testWritable.delete();
                }
                else {
                    path=null;
                }
            }
        }
        if (path!=null)
        {
            sdcardfile=new File(path);

        }
        else {
            sdcardfile=new File(Environment.getExternalStorageDirectory().getAbsolutePath());

        }
        return sdcardfile.getAbsolutePath();
    }

    public String readFromStorage1()
        {
            File dir=new File(getExternalSdCardPath()+"/tut");
            if (dir.exists()){
                File file=new File(getExternalSdCardPath()+"/tut" + "/savedfile.txt");
                if (file.exists())
                {
                    try {

                fileInputStream=getApplicationContext().openFileInput(getExternalSdCardPath()+"/tut/"+"savedfile.txt");
                isr=new InputStreamReader(fileInputStream);
                bufferedReader=new BufferedReader(isr);
                stringBuilder=new StringBuilder();
                while (bufferedReader.readLine()!=null)
                {

                    stringBuilder.append(bufferedReader.readLine());
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
                finally {
                try {
                    if (fileInputStream!=null) {
                        fileInputStream.close();
                    }

                    if (isr!=null) {
                        isr.close();
                    }

                    if (bufferedReader!=null)
                    {bufferedReader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
                }
            else {
                return "Null";
            }}else {
                return "Null";
            }
            if (stringBuilder!=null&&stringBuilder.length()>0)
            {
                return stringBuilder.toString();
            }
            else {
                return "Null";
            }
        }

例外:

java.lang.IllegalArgumentException: File /storage/emulated/0/tut/savedfile.txt contains a path separator
                                                                                                 at android.app.ContextImpl.makeFilename(ContextImpl.java:2663)
                                                                                                 at android.app.ContextImpl.openFileInput(ContextImpl.java:1144)
                                                                                                 at android.content.ContextWrapper.openFileInput(ContextWrapper.java:183)
                                                                                                 at com.globemaster.internalstorage.MainActivity.readFromStorage1(MainActivity.java:190)
                                                                                                 at com.globemaster.internalstorage.MainActivity$2.onClick(MainActivity.java:54)
                                                                                                 at android.view.View.performClick(View.java:5076)
                                                                                                 at android.view.View$PerformClick.run(View.java:20279)
                                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5930)
                                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

0 个答案:

没有答案