Android共享自定义文件
我创建了自己的文件扩展名(.oli)。如果用户单击具有此扩展名的文件,我的应用程序将启动并加载包含的数据。这和预期的一样。问题是我想让我的应用程序的用户有机会共享一个文件(例如:filename.oli) 到目前为止,我实施了这一点:Android共享自定义文件,android,file,android-intent,share,file-extension,Android,File,Android Intent,Share,File Extension,我创建了自己的文件扩展名(.oli)。如果用户单击具有此扩展名的文件,我的应用程序将启动并加载包含的数据。这和预期的一样。问题是我想让我的应用程序的用户有机会共享一个文件(例如:filename.oli) 到目前为止,我实施了这一点: public void shareFile(){ File file = getShareableFile(); //Creates a .oli-file Intent shareIntent = new Intent(Inten
public void shareFile(){
File file = getShareableFile(); //Creates a .oli-file
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri uri = Uri.fromFile(file);
shareIntent.setType("*/*"); //Maybe the problem
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, name);
startActivity(Intent.createChooser(shareIntent, getString(R.string.shareDatei)));
}
问题是,由于shareIntent.setType(“/”),认为可以处理共享我的文件的应用程序的列表非常大;如果您与不同的应用程序共享,以下是两种情况:
如果我选择像Gmail这样的电子邮件应用程序来共享我的文件,那么它应该如何工作。电子邮件包含文件名为filename.oli的文件。当我点击它时,我的应用程序启动了
但是,如果我选择Quickmemo应用程序,我会收到一条消息,该文件不能由该应用程序共享
总而言之,我只想在chooserlist中显示一些应用程序,这些应用程序可以处理以.oli扩展名共享我的文件。我该怎么做?提前谢谢 intent方法实际上使用MIME类型根据上下文过滤应用程序。因此,您只能根据以下筛选器筛选可用的应用程序:
1.文本
sendIntent.setType("text/plain");
二,。二进制的
shareIntent.setType("image/jpeg");
三,。多个内容项
shareIntent.setType("image/*");
编辑
下面是我可能会做的,我会知道我希望哪些应用程序能够共享文件,比如gmail,因为你知道这是可行的,我会选择哪些应用程序在列表中。以下代码来自此SO链接中的答案:
public void onShareClick(视图v){
Resources=getResources();
Intent emailIntent=新Intent();
emailIntent.setAction(Intent.ACTION\u SEND);
//本机电子邮件客户端目前不支持HTML,但如果他们修复了它,尝试一下也无妨
emailIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(resources.getString(R.string.share_email_native));
emailIntent.putExtra(Intent.EXTRA_主题,resources.getString(R.string.share_email_主题));
emailIntent.setType(“message/rfc822”);
PackageManager pm=getPackageManager();
Intent sendIntent=新的Intent(Intent.ACTION\U SEND);
sendIntent.setType(“文本/普通”);
Intent openInChooser=Intent.createChooser(emailIntent,resources.getString(R.string.share\u chooser\u text));
List resInfo=pm.querytentActivities(sendIntent,0);
List intentList=new ArrayList();
对于(int i=0;i
好的,我明白。处理这个问题的最佳方法是什么?如果没有其他应用程序,您最好只为您的扩展名使用自定义ContentProvider过滤器,可能是使用选择器,或者我可能使用文本/纯文本类型,但我不知道您的数据格式。它只是xml代码,但扩展名为.oli。因为如果你点击这个文件,我的应用程序就会通过一个意图过滤器以xml代码启动。这管用!但是如何共享文件是个问题。这很聪明!我喜欢。我用我认为有用的东西编辑了我的答案。您可以自定义选择选择器中包含哪些应用。通过这种方式,您只能选择将用于共享扩展名的应用程序。确定选择器列表包含:电子邮件应用程序、文件浏览器、google drive。这看起来真不错!现在如何添加该文件以及emailIntent的用途是什么,因为它没有被使用???
public void onShareClick(View v) {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
} else if(packageName.contains("facebook")) {
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
} else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}