Olá,
O objectivo é fazer o decode de uma string em Base64 e passar para formato PDF e abrir (não é preciso fazer o download para o dispositivo android).
Neste momento tenho isto:
public void onClick(View view) {
int position = getAdapterPosition();
//Toast.makeText(ctx, "Sapo" + position + base64Strisng, Toast.LENGTH_SHORT).show();
Log.e("Test", base64String.toString());
try {
//Não sei como fazer o decode da string e passar para o itent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
ctx.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
Update 1
@Override
public void onClick(View view) {
int position = getAdapterPosition();
//Toast.makeText(ctx, "Sapo" + position + base64Strisng, Toast.LENGTH_SHORT).show();
Log.e("Test", base64String.toString());
byte[] pdfStream = Base64.decode(base64String, 0);
InputStream inputStream = new ByteArrayInputStream(pdfStream);
File file ;
try {
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "sapo.pdf");
Logger.getLogger("createFile: "+file.getAbsolutePath());
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);
outputStream.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
ctx.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
Update 3
Problema resolvido, estava a fazer de forma errada o decode
O que tinha (errado):
byte[] pdfStream = base64String.getBytes(StandardCharsets.UTF_8);
O que alterei (correcto)
byte[] pdfStream = Base64.decode(base64String, 0);