1

I'm retrieving a picture file as binary code through ajax, then javascript passes it to java on android and android is getting the binary code but I can't make it save it... I have tried many diiferent ways and nothing works.

the code on java so far looks like this:

 public void setFile(String sFileName, String sBody){
    try
    {
        //Log.w("setfile", sBody);
        File root = new File(Environment.getExternalStorageDirectory(), local_address);
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileOutputStream aFileOutStream = new FileOutputStream(gpxfile);
        DataOutputStream aDataOutputStream = new DataOutputStream(aFileOutStream);


        aDataOutputStream.writeUTF(sBody);
        aDataOutputStream.flush();
        aDataOutputStream.close();
        aFileOutStream.close();

        //FileWriter writer = new FileWriter(gpxfile);
        //writer.append(sBody);
        //writer.flush();
        //writer.close();
        //Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    }
   }  

i know java is working because if I uncomment this line

//Log.w("setfile", sBody);

log cat would return the binary code that javascript sent java

3
  • Are you getting anything at all in your local file? Any specific error message? Commented May 9, 2012 at 20:47
  • it is 'log cat' not 'low cat' x) Commented May 9, 2012 at 20:52
  • the filename i want to save the file as. it would be like pic.gif Commented May 9, 2012 at 21:13

3 Answers 3

1

have you set the permission to save on sd card?

https://developer.android.com/guide/topics/data/data-storage.html and https://developer.android.com/reference/android/Manifest.permission.html (https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE)

Sign up to request clarification or add additional context in comments.

1 Comment

yes it is, I can write other text files but it doesn't work with binary
0

I believe you want to use writeBytes() instead of writeUTF() so that it doesn't change your encoding on you.

Comments

0

this one works for me:

public void WriteSettings(Context context, String data){
     FileOutputStream fOut = null;
     OutputStreamWriter osw = null;

     try{
         fOut = openFileOutput("settings.dat", MODE_PRIVATE);      
         osw = new OutputStreamWriter(fOut);

         osw.write(data);
         osw.flush();

         Toast.makeText(context, "Settings saved", Toast.LENGTH_SHORT).show();
     }

      catch (Exception e) {      
         e.printStackTrace();
         Toast.makeText(context, "Settings not saved", Toast.LENGTH_SHORT).show();
      }

      finally {
         try {
             osw.close();
             fOut.close();
         } catch (IOException e) {
                e.printStackTrace();
             }
      }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.