Create a pdf on android

1

I have a byte [] which is a pdf. I use the following class to try to convert it.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_teste);

    byte[] bytes = getIntent().getByteArrayExtra("pdf");

    String fileName = "out.pdf";
    String filePath = Environment.getExternalStorageDirectory().toString();

    try {
        // Create file
        File someFile = new File(filePath, fileName);
        FileOutputStream fos = new FileOutputStream(someFile);
        fos.write(bytes);


        Intent target = new Intent(Intent.ACTION_VIEW);
        target.setDataAndType(Uri.fromFile(someFile), "application/pdf");
        target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

        Intent intent = Intent.createChooser(target, "Open File");
        startActivity(intent);
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
        Toast.makeText(this, "Não foi possível abrir", Toast.LENGTH_LONG).show();
    }

}

But when I open in the pdf reader it says that the file is corrupted or damaged.

    
asked by anonymous 21.05.2018 / 21:31

1 answer

2

Solved in a simple way. The Code was practically right, all it needed was the base64 decode. I added this line after getting the byte [] from previous activity

byte[] decodedString = Base64.decode(bytes, Base64.DEFAULT);
    
22.05.2018 / 15:53