2016-07-08 49 views
0

我从选定的pdf中创建了bytearray。我正在创建contentprovider,将这个bytearray附加为gmail附件,然后发送邮件。邮件接收成功时具有相同的文件大小,当我尝试使用Adobe Reader打开文件时,它会打开文件并显示否。 (例如:1/3),但不显示任何页面的内容(显示空白/白页)。我甚至没有得到任何错误。我使用下面的代码...android中字节数组的内容提供者

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(); 
+0

内容是PDF文件的字节数组? –

+0

yes .. bytearray使用inputstream创建..使用以下代码......... public byte [] returnPDFBytes(InputStream inStream) byte [] tempbytes = null; try { BufferedInputStream bis = new BufferedInputStream(inStream); DataInputStream dis = new DataInputStream(bis); tempbytes =新字节[dis.available()]; int i = 0; (dis.available()> 0)tempbytes [i] = dis.readByte(); i ++; } return tempbytes; } –

回答

0

它看起来像你写一个字符串到你的输出文件。我会建议使用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(); 
+0

它不工作..如果使用fileoutputstream应用程序本身崩溃...如果内容(bytearray)直接写入附件只有1 kb ...不是数据... –

+0

它有什么区别吗?因为我正在用inputstream阅读pdf。我不想使用物理路径(文件),并有可能有大型PDF ..我正在使用while循环... –

0
//if send email button clicked 
     //check bytes and attach to mail 
     if (bytes.length > 0) { 
      try { 
       createCachedFile(this, attachmentFileName, bytes); 
       this.startActivity(getSendEmailIntent(this, "[email protected]", "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; 
} 
相关问题