wake-up-neo.com

Speichern auf SD-Karte als Textdatei

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

Ich versuche, Gyroskop-Daten in einer TXT-Datei zu speichern, aber es wird nicht gespeichert. Wenn jemand das Problem findet, helfen Sie mir bitte, es zu korrigieren.

12
user2967906

probieren Sie diesen Code für mich aus.

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();
            Toast.makeText(getBaseContext(),
                    "Done writing SD 'mysdfile.txt'",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }

vergiss nicht, die Erlaubnis zu nehmen

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

Versuchen Sie diesen Code:

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

Denken Sie auch daran, die Berechtigung für den externen Speicher in Ihre Manifestdatei aufzunehmen:

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

und auf Android 6.0, um um Erlaubnis für WRITE_EXTERNAL_STORAGE zu bitten

13
Gil Moshayof

Hast du die Erlaubnis benutzt, in sd zu schreiben?

<uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE" />
1
Gabriel Moreno

Deklarieren Sie diese Berechtigung zunächst für Ihre androidManifest.xml:

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

Dann schreiben Sie diesen Code auf Ihre Schaltflächenklickereignisse

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

Versuche dies..

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

und auch externe Speichererlaubnis in Ihrer Manifestdatei:

<uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE" />
0
Hariharan

Ich mache das so

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

Auch diese Erlaubnis ist erforderlich

<uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE" />
0
Rohit5k2