Writing a file to sdcard

后端 未结 3 1775
野性不改
野性不改 2020-11-27 05:44

I\'m trying to write a file from an Http post reply to a file on the sdcard. Everything works fine until the byte array of data is retrieved.

相关标签:
3条回答
  • 2020-11-27 05:48
    //------------------------------WRITING DATA TO THE FILE ---------------------------------      
    
    btnWriteSDFile.setOnClickListener(new OnClickListener() 
        {
        public void onClick(View v)
        {       
    
            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(v.getContext(),"Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show();
                txtData.setText("");
            } 
            catch (Exception e) 
            {
                Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
            }
        }
    
    
        }); 
    
    //---------------------------READING DATA FROM THE FILE PLACED IN SDCARD-------------------//       
            btnReadSDFile.setOnClickListener(new OnClickListener()
            {
    
            public void onClick(View v) 
            {
    
            try {
    
                File myFile = new File("/sdcard/mysdfile.txt");
                FileInputStream fIn = new FileInputStream(myFile);
                BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
                String aDataRow = "";
                String aBuffer = "";
                while ((aDataRow = myReader.readLine()) != null) 
                {
                    aBuffer += aDataRow ;
                }
                txtData.setText(aBuffer);
                myReader.close();
                Toast.makeText(v.getContext(),"Done reading SD 'mysdfile.txt'",Toast.LENGTH_SHORT).show();
            } 
            catch (Exception e)
            {
                Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
            }
            }
            }); 
    

    ALONG WITH THIS ALSO WRITE THIS PERMISSION IN Android.Manifest

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2020-11-27 06:11

    Here is a sample:

    // Log used as debug
    File log = new File(Environment.getExternalStorageDirectory(), "Log.txt");
    try {
        out = new BufferedWriter(new FileWriter(log.getAbsolutePath(), false));
        out.write(new Date().toString());
        out.write(" : \n");
    } catch (Exception e) {
        Log.e(TAG, "Error opening Log.", e);
    }
    
    0 讨论(0)
  • 2020-11-27 06:12

    The openFileOutput() method writes data to your application's private data area (not the SD card), so that's probably not what you want. You should be able to call Environment.getExternalStorageDirectory() to get the root path to the SD card and use that to create a FileOutputStream. From there, just use the standard java.io routines.

    0 讨论(0)
提交回复
热议问题