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.