• <legend id='mamDx'><style id='mamDx'><dir id='mamDx'><q id='mamDx'></q></dir></style></legend>

        <tfoot id='mamDx'></tfoot>

        <small id='mamDx'></small><noframes id='mamDx'>

          <bdo id='mamDx'></bdo><ul id='mamDx'></ul>

        <i id='mamDx'><tr id='mamDx'><dt id='mamDx'><q id='mamDx'><span id='mamDx'><b id='mamDx'><form id='mamDx'><ins id='mamDx'></ins><ul id='mamDx'></ul><sub id='mamDx'></sub></form><legend id='mamDx'></legend><bdo id='mamDx'><pre id='mamDx'><center id='mamDx'></center></pre></bdo></b><th id='mamDx'></th></span></q></dt></tr></i><div id='mamDx'><tfoot id='mamDx'></tfoot><dl id='mamDx'><fieldset id='mamDx'></fieldset></dl></div>

        在 Android 的外部存储中写入文件

        时间:2023-08-29

          1. <small id='Xb2Z2'></small><noframes id='Xb2Z2'>

            1. <i id='Xb2Z2'><tr id='Xb2Z2'><dt id='Xb2Z2'><q id='Xb2Z2'><span id='Xb2Z2'><b id='Xb2Z2'><form id='Xb2Z2'><ins id='Xb2Z2'></ins><ul id='Xb2Z2'></ul><sub id='Xb2Z2'></sub></form><legend id='Xb2Z2'></legend><bdo id='Xb2Z2'><pre id='Xb2Z2'><center id='Xb2Z2'></center></pre></bdo></b><th id='Xb2Z2'></th></span></q></dt></tr></i><div id='Xb2Z2'><tfoot id='Xb2Z2'></tfoot><dl id='Xb2Z2'><fieldset id='Xb2Z2'></fieldset></dl></div>
                <bdo id='Xb2Z2'></bdo><ul id='Xb2Z2'></ul>

                <tfoot id='Xb2Z2'></tfoot>
                    <tbody id='Xb2Z2'></tbody>

                  <legend id='Xb2Z2'><style id='Xb2Z2'><dir id='Xb2Z2'><q id='Xb2Z2'></q></dir></style></legend>
                  本文介绍了在 Android 的外部存储中写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在外部存储 sdCard 中创建一个文件并写入它.我已经通过互联网搜索并尝试但没有得到结果,我也在 Android Manifest 文件中添加了权限,我正在模拟器上执行此操作,我我正在尝试以下代码并得到一个 ERRR", "Could not create file".

                  I want to create a file in external storage sdCard and write to it.I have searched through internet and try but not getting the result,I have added permission in Android Manifest file as well,I am doing this on Emulator,I am trying the following code and getting a ERRR", "Could not create file".

                  btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
                  btnWriteSDFile.setOnClickListener(new OnClickListener() {
                      //private Throwable e;
                  
                      @Override
                      public void onClick(View v) {
                          // write on SD card file data from the text box
                          try {
                              File myFile = new File("/sdcard/mysdfile.txt");
                              myFile.createNewFile();
                              FileOutputStream fOut = new FileOutputStream(myFile);
                              OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
                              myOutWriter.append(txtData.getText());
                              myOutWriter.close();
                              fOut.close();
                  
                          } catch (Exception e) {
                                Log.e("ERRR", "Could not create file",e);
                          } 
                      }// onClick
                  }); // btnWriteSDFile
                  

                  推荐答案

                  你也可以用这段代码来做到这一点.

                  You can do this with this code also.

                   public class WriteSDCard extends Activity {
                  
                   private static final String TAG = "MEDIA";
                   private TextView tv;
                  
                    /** Called when the activity is first created. */
                  @Override
                   public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.main);     
                      tv = (TextView) findViewById(R.id.TextView01);
                      checkExternalMedia();
                      writeToSDFile();
                      readRaw();
                   }
                  
                  /** Method to check whether external media available and writable. This is adapted from
                     http://developer.android.com/guide/topics/data/data-storage.html#filesExternal */
                  
                   private void checkExternalMedia(){
                        boolean mExternalStorageAvailable = false;
                      boolean mExternalStorageWriteable = false;
                      String state = Environment.getExternalStorageState();
                  
                      if (Environment.MEDIA_MOUNTED.equals(state)) {
                          // Can read and write the media
                          mExternalStorageAvailable = mExternalStorageWriteable = true;
                      } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
                          // Can only read the media
                          mExternalStorageAvailable = true;
                          mExternalStorageWriteable = false;
                      } else {
                          // Can't read or write
                          mExternalStorageAvailable = mExternalStorageWriteable = false;
                      }   
                      tv.append("
                  
                  External Media: readable="
                              +mExternalStorageAvailable+" writable="+mExternalStorageWriteable);
                  }
                  
                  /** Method to write ascii text characters to file on SD card. Note that you must add a 
                     WRITE_EXTERNAL_STORAGE permission to the manifest file or this method will throw
                     a FileNotFound Exception because you won't have write permission. */
                  
                  private void writeToSDFile(){
                  
                      // Find the root of the external storage.
                      // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal
                  
                      File root = android.os.Environment.getExternalStorageDirectory(); 
                      tv.append("
                  External file system root: "+root);
                  
                      // See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
                  
                      File dir = new File (root.getAbsolutePath() + "/download");
                      dir.mkdirs();
                      File file = new File(dir, "myData.txt");
                  
                      try {
                          FileOutputStream f = new FileOutputStream(file);
                          PrintWriter pw = new PrintWriter(f);
                          pw.println("Hi , How are you");
                          pw.println("Hello");
                          pw.flush();
                          pw.close();
                          f.close();
                      } catch (FileNotFoundException e) {
                          e.printStackTrace();
                          Log.i(TAG, "******* File not found. Did you" +
                                  " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
                      } catch (IOException e) {
                          e.printStackTrace();
                      }   
                      tv.append("
                  
                  File written to "+file);
                  }
                  
                  /** Method to read in a text file placed in the res/raw directory of the application. The
                    method reads in all lines of the file sequentially. */
                  
                  private void readRaw(){
                      tv.append("
                  Data read from res/raw/textfile.txt:");
                      InputStream is = this.getResources().openRawResource(R.raw.textfile);
                      InputStreamReader isr = new InputStreamReader(is);
                      BufferedReader br = new BufferedReader(isr, 8192);    // 2nd arg is buffer size
                  
                      // More efficient (less readable) implementation of above is the composite expression
                      /*BufferedReader br = new BufferedReader(new InputStreamReader(
                              this.getResources().openRawResource(R.raw.textfile)), 8192);*/
                  
                      try {
                          String test;    
                          while (true){               
                              test = br.readLine();   
                              // readLine() returns null if no more lines in the file
                              if(test == null) break;
                              tv.append("
                  "+"    "+test);
                          }
                          isr.close();
                          is.close();
                          br.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                      tv.append("
                  
                  That is all");
                  }
                  }
                  

                  这篇关于在 Android 的外部存储中写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何获取 Android 应用程序的构建/版本号? 下一篇:RuntimeException:无法实例化应用程序

                  相关文章

                  最新文章

                • <i id='jMuKe'><tr id='jMuKe'><dt id='jMuKe'><q id='jMuKe'><span id='jMuKe'><b id='jMuKe'><form id='jMuKe'><ins id='jMuKe'></ins><ul id='jMuKe'></ul><sub id='jMuKe'></sub></form><legend id='jMuKe'></legend><bdo id='jMuKe'><pre id='jMuKe'><center id='jMuKe'></center></pre></bdo></b><th id='jMuKe'></th></span></q></dt></tr></i><div id='jMuKe'><tfoot id='jMuKe'></tfoot><dl id='jMuKe'><fieldset id='jMuKe'></fieldset></dl></div>

                  <small id='jMuKe'></small><noframes id='jMuKe'>

                    <tfoot id='jMuKe'></tfoot>

                    • <bdo id='jMuKe'></bdo><ul id='jMuKe'></ul>

                      <legend id='jMuKe'><style id='jMuKe'><dir id='jMuKe'><q id='jMuKe'></q></dir></style></legend>