[英]Content provider for bytearray in android
我有从选定的pdf创建的字节数组。 我正在创建contentprovider将此字节数组作为gmail附件附加,然后发送邮件。 邮件成功接收且具有相同的文件大小,当我尝试使用Adobe Reader打开文件时,它将打开文件并显示“否”。 页数(例如1/3),但不显示任何页面的内容(显示空白页)。 我什至没有任何错误。 我正在使用以下代码...
File cacheFile = new File(context.getCacheDir() + File.separator + fileName);
cacheFile.createNewFile();
String str = new String(content);
//FileOutputStream fos = new FileOutputStream(cacheFile);
OutputStream osw = new FileOutputStream(cacheFile);
//ByteArrayOutputStream osw = new ByteArrayOutputStream(content.length);
PrintWriter pw = new PrintWriter(osw);
//Toast.makeText(this, content, Toast.LENGTH_LONG).show();
//pw.println(content);
pw.println(str);
pw.flush();
pw.close();
看起来您将String写入输出文件中。 我会建议改用write()。 例:
byte[] content;
//...
File cacheFile = new File(context.getCacheDir() + File.separator + fileName);
FileOutputStream f = new FileOutputStream(cacheFile);
f.write(content);
f.flush();
f.close();
编辑:
根据您的答复,是否真的需要使用while循环打开输入文件? 您是否尝试过这样的方法来接收您输入的pdf文件字节数组:
File inputPDF = new File("your.pdf");
byte[] inputPDFData = new byte[(int) inputPDF.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(inputPDF));
dis.readFully(inputPDFData);
dis.close();
//if send email button clicked
//check bytes and attach to mail
if (bytes.length > 0) {
try {
createCachedFile(this, attachmentFileName, bytes);
this.startActivity(getSendEmailIntent(this, "testuurmi@gmail.com", "Test", "See attached", attachmentFileName));
} catch (IOException e) {
e.printStackTrace();
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Gmail is not available on this device.", Toast.LENGTH_SHORT).show();
}
}
public void createCachedFile(Context context, String fileName, byte[] content) throws IOException {
File cacheFile = new File(context.getCacheDir() + File.separator + fileName);
cacheFile.createNewFile();
BufferedOutputStream bostream = new BufferedOutputStream(new FileOutputStream(cacheFile));
bostream.write(content);
bostream.flush();
bostream.close();
}
public static Intent getSendEmailIntent(Context context, String email, String subject, String body, String fileName) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
//Explicitly only use Gmail to send
// emailIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
//emailIntent.setType("message/rfc822");
emailIntent.setType("vnd.android.cursor.item/email");
//Add the recipients
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { email });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
//Add the attachment to custom ContentProvider
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + AttachFileProvider.AUTHORITY + "/" + fileName));
return emailIntent;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.