本文介绍了机器人创建可以附加到电子邮件的存储器驻留输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后的目标是明确的很快。

The final objective will be clear shortly.

我想创建一个文件对象,而不是从一个真实的物理文件中获取数据我想提供缓冲自己。

I want to create a file object and instead of getting data from a real physical file I want to provide the buffer myself.

然后,我想用这个文件,它并不真正存在于SD卡或我的应用程序以外的任何地方,给它一个名称,并通过电子邮件作为附件(使用EXTRA_STREAM)发送。

Then, I want to use this file, which does not really exist in the sdcard or anywhere outside my app, give it a name and send it by email as an attachment (using the EXTRA_STREAM).

我发现,code以下位,由阿德里安·科斯特(@adriaankoster)后的

I found the following bit of code, by Adriaan Koster (@adriaankoster), the post Write byte[] to File in Java

// convert byte[] to File
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
File fileFromBytes = (File) ois.readObject();
bis.close();
ois.close();

System.out.println(fileFromBytes);

我用它来创建此功能

I used it to create this function

private File fileFromBytes(byte[] buf) {
    File f = null;
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(buf);
        ObjectInputStream ois = new ObjectInputStream(bis);
        f = (File) ois.readObject();
        bis.close();
        ois.close();
    }
    catch (Exception e) {}
    return f;
}

和这里就是我坚持,因为当我使用它:

and here is where I am stuck, because when I use it:

// When sent as body the mail is sent OK
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, dump());

// When I try to attach the mail is empty
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, fileFromBytes(dump().getBytes()));

我知道,从我见过的第二个参数应该是一个URI的例子,但是:如何创建一个虚拟的URI,以适应我的文件

I know from examples I've seen the second argument should be an URI, but: How do I create a virtual URI to fit my file?

编辑:该选项可直接从内部应用程序是很重要的某一种应用程序附加数据。也就是说,安全与放大器;那些不想走动太多的敏感数据的银行应用程序。当然,如果该数据未到达的SD卡,并直接进入到邮件附件则很难嗅出比应用程序存储器中。
这不是我的具体情况,但我想指出的是,这种能力是非常重要的有。

The option to attach data directly from within the application is important to certain kind of applications. Namely, security & banking applications that do not want to move sensitive data around too much. Surely if the data does not reach the sdcard and goes directly to a mail attachment it is harder to sniff than within the application memory.
This is not my specific case, but I wanted to point out that this capability is important to have.

推荐答案

你会想要做的第一件事,我想,就是创建一个ContentProvider的。你可以在这里看到一个例子实施

The first thing you'll want to do, I imagine, is create a ContentProvider. You can see an example implementation here

凡在上面的链接的情况下,你会添加到您的Andr​​oidManifest.xml

where in the above link's case, you would add this to your AndroidManifest.xml

<provider
    android:name="org.tsg.web.WebContentProvider"
    android:authorities="your.package.name" />

现在,你将有一个内容URI可供使用,内容://your.package.name/

Now, you'll have a content uri available for use, content://your.package.name/.

以上的ContentProvider你的,有兴趣的我再次想象的部分,是中openFile 方法。当通过在应用程序的意图共享数据,某些事情的预期。在你的情况,你想分享的意思是附加到电子邮件一些字节的数据。

The portion of the above ContentProvider your interested in, again I imagine, is the openFile method. When sharing data by intent across apps, certain things are expected. In your case, you're looking to share some byte data that's meant to be attached to the email.

所以,如果你传递一个内容URI的电子邮件应用程序,如内容://your.package.name/foo 与相应的意图的标志,那么中openFile 将调用您的ContentProvider。在这种情况下,您可以检查URI段的末尾看到请求,并返回适当的。

So if you pass in a content uri to the email app such as content://your.package.name/foo with the appropriate intent flags, then openFile will get called on your ContentProvider. In this case, you can inspect the end of the uri segment to see foo was requested, and return appropriately.

您带来了下一个问题是不具有实际的文件在磁盘上。虽然我不能保证你上面所用的方法(虽然它看起来洁净),你必须返回一个 ParcelFileDescriptor 从您的ContentProvider。如果你看看我提供的链接,你可能尝试使用它作为一个样本,从中获取文件描述符的文件对象(我的知识弃权这里),但我想象一下,该数据只是不会提供在这一点上。

The next issue you bring up is not having the file actually on disk. While I can't vouch for the method you used above (though it looks kosher), what you need to be returning is a ParcelFileDescriptor from your ContentProvider. If you look at the link I provided, you could possibly try to use that as a sample to get the file descriptor from your File object (my knowledge waivers here), but I imagine, the data simply wont be available at that point.

你做什么提出的是安全,但。重要的是要注意,您可以将数据写入到磁盘上私下因此只有应用程序可以访问数据。我相信,但你可能要仔细检查这一点,如果数据是私有的应用程序,您可以通过ContentProvider的暴露,并可能锁定谁,以及如何提供者被使用,谁可以调用它,等你可能想挖成Android的文档该部分或看一些其他等问题。

What you do bring up is security though. It's important to note that you can write data to disk privately so only the app has access to the data. I believe, but you might want to double check on this, if that data is private to the app, you can expose it via the ContentProvider and possibly lock down who and how the provider gets used, who can call it, etc. You may want to dig into android docs for that portion or look at some other SO questions.

总之,祝你好运。

这篇关于机器人创建可以附加到电子邮件的存储器驻留输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 17:12