Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a file to sdcard

Tags:

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.

I've tried setting WRITE_EXTERNAL_STORAGE permission in the manifest and tried many different combinations of tutorials I found on the net.

All I could find was using the openFileOutput("",MODE_WORLD_READABLE) method, of the activity but how my app writes file is by using a thread. Specifically, a thread is invoked from another thread when a file has to be written, so giving an activity object didn't work even though I tried it.

The app has come a long way and I cannot change how the app is currently written.

Please, someone help me?


CODE:

File file = new File(bgdmanip.savLocation); FileOutputStream filecon = null; filecon = new FileOutputStream(file);  byte[] myByte; myByte = Base64Coder.decode(seReply);  bos.write(myByte); filecon.write(myByte); myvals = x * 11024; 

bgdmanip.savLocation holds the whole files path. seReply is a string reply from HttpPost response. The second set of code is looped with reference to x. The file is created but remains 0 bytes.

like image 653
Sumit M Asok Avatar asked Mar 16 '10 14:03

Sumit M Asok


People also ask

How do I view files on my SD card?

If you have an SD card mounted on your device, then you can easily read & write files to the SD card from Office on Android apps. On the Open page, tap This device. Tap SD Card or Documents (SD Card).


1 Answers

//------------------------------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" /> 
like image 92
Pir Fahim Shah Avatar answered Oct 11 '22 15:10

Pir Fahim Shah