【问题标题】:Saving to SD card as text file作为文本文件保存到 SD 卡
【发布时间】:2013-11-20 03:05:53
【问题描述】:
   stopWriting = (Button) findViewById(R.id.save);
   stopWriting.setOnClickListener(new OnClickListener() {

       @SuppressLint("SdCardPath")
    public void onClick(View v) {
           // stop recording the sensor data
           try {

               myFile = new File("/sdcard/SensorData/" + txtData.getText() + ".txt");
               myFile.createNewFile();

               sData = new FileOutputStream(myFile);
               myOutWriter = new OutputStreamWriter(sData);
               myBufferedWriter = new BufferedWriter(myOutWriter);
               myPrintWriter = new PrintWriter(myBufferedWriter);

               //if(myFile != null )//stopFlag = mSensorManager.cancelTriggerSensor(null, null);
               Toast.makeText(getBaseContext(), "Done", Toast.LENGTH_SHORT).show();
           } catch (Exception e) {
               Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
           }
       }
   });

我正在尝试将陀螺仪数据保存到 txt 文件,但它没有保存。如果有人发现问题,请帮我纠正。

【问题讨论】:

    标签: java android save android-sdcard android-memory


    【解决方案1】:

    试试这个代码:

    public  void writeToFile(String fileName, String body)
        {
            FileOutputStream fos = null;
    
            try {
                final File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/folderName/" );
    
                if (!dir.exists())
                {
                         if(!dir.mkdirs()){
                             Log.e("ALERT","could not create the directories");
                         }
                }
    
                final File myFile = new File(dir, fileName + ".txt");
    
                if (!myFile.exists()) 
                {    
                    myFile.createNewFile();
                } 
    
                fos = new FileOutputStream(myFile);
    
                fos.write(body.getBytes());
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    

    另外,请记住在清单文件中包含外部存储权限:

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

    在 android 6.0 上请求 WRITE_EXTERNAL_STORAGE 的权限

    【讨论】:

      【解决方案2】:

      首先向您的 androidManifest.xml 声明此权限:

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

      然后在你的按钮点击事件上写下这段代码

       try {
             putStream fOut = new FileOutputStream(myFile);
              OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
              myOutWrite File myFile = new File("/sdcard/SensorData.txt");
              myFile.createNewFile();
              FileOutr.append(txtData.getText());
              myOutWriter.close();
              fOut.close();
              Toast.makeText(v.getContext(),"Done writing SD 'SensorData.txt'", Toast.LENGTH_SHORT).show();
              txtData.setText("");
          } 
          catch (Exception e) 
          {
              Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
          }
      }
      

      【讨论】:

        【解决方案3】:

        试试这个..

        stopWriting.setOnClickListener(new OnClickListener() {
        
               @SuppressLint("SdCardPath")
            public void onClick(View v) {
                   // stop recording the sensor data
                   try {
        
                       myFile = new File("/sdcard/SensorData/" + txtData.getText() + ".txt");
                        myFile.createNewFile();
                    FileOutputStream fOut = new FileOutputStream(myFile);
                    OutputStreamWriter myOutWriter = 
                                            new OutputStreamWriter(fOut);
                    myOutWriter.append(txtData.getText());
                    myOutWriter.close();
                    fOut.close();
                    Toast.makeText(getBaseContext(),
                            "Done writing SD " + txtData.getText() + ".txt",
                            Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }
        }
           });
        

        以及清单文件中的外部存储权限:

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

        【讨论】:

          【解决方案4】:

          我就是这样做的

          public void write() 
          {
              FileOutputStream output = null;
          
              try
              {
                  String mediaState = Environment.getExternalStorageState();
                  if(mediaState.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
                  {
                      output = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/download/shared.txt"));
          
                      String str = "Hello, writing to a text file";
                      byte[] b = encrypt2(str);
                      output.write(b);
                  }
                  else 
                  {
                      // error
                  }
              }
              catch (IOException e)
              {
                  e.printStackTrace();
              }
              finally
              {
                  try
                  {
                      if (output != null)
                          output.close();
                  }
                  catch (IOException e)
                  {
                      e.printStackTrace();
                  }
              }
          }
          
          public static byte[] encrypt2(String value)
          {
              byte[] input = value.getBytes();
              int nLen = input.length;
          
              if(nLen % 2 != 0)
              {
                  nLen--;
              }
          
              byte temp;
              for (int i = 0; i < nLen; i += 2)
              {
                  temp = input[i+1];
                  input[i+1] = input[i];
                  input[i] = temp;
              }
              // do encryption
              return Base64.encode(input, Base64.NO_WRAP);
          }
          

          此权限也是必需的

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

          【讨论】:

            【解决方案5】:

            你是否使用了权限在 sd 中写入?

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-09-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-06-08
              相关资源
              最近更新 更多