1

I convert one file in byte array in server and send as a by json string to Android Client.. by this code i convert that file :

FileInputStream fileInputStream=null;

        File file = new File("C:\\testing.txt");

        byte[] bFile = new byte[(int) file.length()];

        try {
            //convert file into array of bytes
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();
}catch(Exception e){
            e.printStackTrace();
        }

and in Android Client i got the value like : "SGVsbG8gRG93bmxvYWQgaXMgd29ya2luZw==" (In String Type)

So how i convert this code into byte and convert in file and save in sd card?

rciovati
  • 27,603
  • 6
  • 82
  • 101
Akash Kambad
  • 11
  • 1
  • 2
  • 1
    This is base64. Try and see if the Android JDK has a method for reading this kind of stuff and converting it to binary. – fge Jun 17 '13 at 13:53
  • http://stackoverflow.com/questions/15470221/convert-binary-stream-into-byte/15470323#15470323 hope this will give you some solution.. – Nirmal Jun 17 '13 at 14:02

2 Answers2

3

You have binary data encoded using BASE64 encoding.

To decode it you can use android.util.Base64 class.

To learn how to write file to an external store read this article.

loentar
  • 5,111
  • 1
  • 24
  • 25
1
try {
     /* file_byte is yous json string*/
    byte[] decodestring = Base64.decode(file_byte, Base64.DEFAULT);
    File file = Environment.getExternalStorageDirectory();
    File dir = new File(file.getAbsolutePath() + "/VPM/Document/");
     if (!dir.exists()) {
         dir.mkdirs();
     }
     File document = new File(dir, doc_name);

     if (document.exists()) {
         document.delete();
      }

     FileOutputStream fos = new FileOutputStream(document.getPath());
      fos.write(decodestring);
      fos.close();
   } catch (Exception e) {
    Log.e(TAG, "error: " + e.getMessage());
    runOnUiThread(new Runnable() {
    @Override
        public void run() {
            Snackbar.make(root_doc, "Failed to download file..", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show();
        }
    });
}