0

I am trying to first select pdf file, then encode it to base64 and save it to shared preference. After that, decode and save to pdf. And open that pdf to view.

When opening it displays message "This document cannot be opened". It looks like there was some problem on encoding/decoding part. I don't know where. So, if anyone has idea on it, It would be great help for me. Thanks,

My code is as follows:

Encoding :

public static String getBase64Pdf(String pdfFilePath){
    try{
        InputStream inputStream = new FileInputStream(pdfFilePath);
        byte[] byteArray = IOUtils.toByteArray(inputStream);
        String encodedPdfString = Base64.encodeToString(byteArray, Base64.DEFAULT);
        PrintLog.showTag(TAG,"pdf converted to string : " + encodedPdfString);
        return encodedPdfString;
    }catch (IOException e){
    }
    return "";
}

Decoding, saving to file and open pdf :

private void openPdfViewer(){
    try{
        PrintLog.showTag(TAG,"== opnPdfViewer & length of base64 string is =="
                + sessionManager.getBase64ProofImage().length());

        //== decode and write file here
        String base64pdf = sessionManager.getBase64ProofImage();//gets base64 from shared preference
        final File newFilePath = new File(getActivity().getFilesDir(), "warrantyProof.pdf");
        byte[] pdfAsBytes = Base64.decode(base64pdf, Base64.NO_WRAP);
        FileOutputStream os;
        os = new FileOutputStream(newFilePath, false);
        os.write(pdfAsBytes);
        os.flush();
        os.close();

        //==== open pdf file here
        File file = new File(getActivity().getFilesDir(),"warrantyProof.pdf");

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
    }catch (IOException e){
        PrintLog.showException(TAG,"IO exception saving pdf ",e);
    }
    catch (Exception e){
        PrintLog.showException(TAG,"exception opening pdf",e);
    }
}
Binod Lama
  • 226
  • 5
  • 24
  • you want to save the whole pdf content in shared preference? are you really sure? the docs say: `"If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs."` your pdf isnn't rather small, is it? – pskink Jun 13 '17 at 10:28
  • The pdf will be 1/2 page only and will be checked for max memory. I have stored base64 image data to sharedPreference and it is working well. Can this be memory problem of shared Preference ? – Binod Lama Jun 13 '17 at 10:35
  • 1
    shared prefs are not designed for things like that, more on storage options here: https://developer.android.com/guide/topics/data/data-storage.html – pskink Jun 13 '17 at 10:42
  • Ok, I will find a way to store base64 string to another medium (database or directly pass to server api through arguments). Thanks, and do you have any idea on above encoding/decoding part. I mean like, if there is any mistake or issue there. – Binod Lama Jun 13 '17 at 10:51
  • 1
    what do you need this base64 for? it only enlarges your data by 33%... – pskink Jun 13 '17 at 10:52
  • I need to pass pdf/image as base64 string to server api as my current project. Backend developers has designed api in that way. If it doesnot succeed, then may be we will try url way. But, first I need to try passing base64. – Binod Lama Jun 13 '17 at 10:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146523/discussion-between-binod-lama-and-pskink). – Binod Lama Jun 13 '17 at 11:13

1 Answers1

0
 File dir = Environment.getExternalStorageDirectory();
    File yourFile = new File(dir, "path/to/the/file/inside/the/myfile.ext");
    String encodeFileToBase64Binary = encodeFileToBase64Binary(yourFile);

    private static String encodeFileToBase64Binary(File fileName) throws IOException {
          byte[] bytes = loadFile(fileName);
          byte[] encoded = Base64.encodeBase64(bytes);
          String encodedString = new String(encoded);
          return encodedString;
     }
Akshay GS
  • 93
  • 6