试图在我的EditText中打开sdcard的.txt文件

时间:2014-03-24 07:31:37

标签: android

我正在尝试在我的EditText中打开sdcard的.txt文件来更改其中的文字 使用按钮点击保存更改,但它没有正确的方式。可能我知道实现目标的正确方法是什么?

3 个答案:

答案 0 :(得分:0)

某些制造商(主要是三星)仅向/storage/sdcard处安装的内部存储授予写入权限。

此外,您应该通过在清单中添加此权限来启用应用程序以写入外部存储: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

            try this one..

you must set permission in the manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  


         File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()                          + "followed by ur file dir");

         File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()                          + "sdcard.txt");

          for (File wavfile : f.listFiles())
         {
            String str = wavfile.getName().toString();   
         StringBuffer stringBuffer = new StringBuffer();    
                        String aDataRow = "";  
                        String aBuffer = "";  
                        try {  
                            File myFile = new File("/sdcard/"+filename);  
                            FileInputStream fIn = new FileInputStream(myFile);  
                            BufferedReader myReader = new BufferedReader(  
                                    new InputStreamReader(fIn));  

                            while ((aDataRow = myReader.readLine()) != null) {  
                                aBuffer += aDataRow + "\n";  
                            }  
                            myReader.close();  
        edtitext.setText(aBuffer.tostring());

                        } catch (IOException e) {  
                            e.printStackTrace();  
                        }  
                   Toast.makeText(getApplicationContext  

        (),aBuffer,Toast.LENGTH_LONG).show();  

                    }       
        }
        again btnclick you have to write that edited string right..



        use this one for write in that file..
        FileOutputStream fos;  
                           try {  
                               File myFile = new File("/sdcard/"+sdcard.txt);  
                                myFile.createNewFile();  
                                FileOutputStream fOut = new   

        FileOutputStream(myFile);  
                                OutputStreamWriter myOutWriter = new   

        OutputStreamWriter(fOut);  
                                myOutWriter.append(data);  
                                myOutWriter.close();  
                                fOut.close();  

                    Toast.makeText(getApplicationContext(),filename + "   

        saved",Toast.LENGTH_LONG).show();  


                           } catch (FileNotFoundException e) {e.printStackTrace();}  
                           catch (IOException e) {e.printStackTrace();}  

                    }   

    thank you...


    more detalis ref this link'

    http://www.javatpoint.com/android-external-storage-example
    ----------------------------------------------------------

答案 2 :(得分:-1)

首先尝试阅读完整的文本文件并将文本保存为字符串。

为此,您可以参考此link

当您阅读完整的文本文件并获取字符串中的文本时,请将字符串设置为editText

edtitext.setText(your string);