1
I have a byte[] which is a pdf. 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 pdf reader it says that the file is corrupted or damaged.
Navigating through the mobile file manager, you found the created pdf?
– LMaker
I can’t find the app folder in the emulator
– Marceloawq
Checks if you have permissions
WRITE_EXTERNAL_STORAGE
andREAD_EXTERNAL_STORAGE
– LMaker
is with the guaranteed permissions
– Marceloawq
I changed the destination folder to sd card, then it says pdf is corrupted
– Marceloawq
It may be Android security permission, try using the following lines of code:
String fileName = "fileName.pdf"; File file = new File(Environment.getExternalStorageDirectory(), fileName);
perhaps using theEnvironment
for sure– Gaspar
@Gaspar did so but at the time of opening says the document is damaged or corrupted
– Marceloawq
I didn’t understand the line
byte[] bytes = getIntent().getByteArrayExtra("pdf");
thegetIntent()
take the current Activity Intent, you may need to pull the Intent from an external method, check this answer– Gaspar
@Gaspar this line is right, it takes the byte[] of the previous Activity. I updated the question with the new code
– Marceloawq