我正在尝试使用以下代码共享应用程序的屏幕截图:
View content = findViewById(R.id.layoutHome);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory,"temp.png");
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String url = "file://" + sdCardDirectory.toString() + "Images/temp.png";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, url); …Run Code Online (Sandbox Code Playgroud) 我试图通过Twitter应用程序分享一些文本和图像.图像源是web url.以下是我的代码:
sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("*/*");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "I am reading this publication, check it out here: "+bookmark_url+"");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("http://example.com/images/thumbs/file_name.jpg"));
Run Code Online (Sandbox Code Playgroud)
但只有文本通过Twitter应用程序共享.我还收到一条消息" 无法加载图片 ".这段代码有什么问题?
有没有办法确认Android中的共享意图是成功还是不成功?(例如,如果我分享Facebook帖子,我想知道它是否已成功发布或知道它是否被取消.)
以下是我用来分享的Android意图代码.目前,它会启动一个对话框,允许用户选择要共享的应用程序:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
activity.startActivity(Intent.createChooser(shareIntent, title));
Run Code Online (Sandbox Code Playgroud) 我无法在 Android 中使用 Intent 在 Instagram 提要上分享任何照片。它打开 Instagram 并显示“无法加载照片”错误并返回到我的应用程序。
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, "Share to"));
Run Code Online (Sandbox Code Playgroud)
相同的代码适用于 Instagram 版本 131.0.0.23.116,但不适用于当前更新的版本 134.0.0.26.121
我一直在尝试解决从外部存储目录共享图像的问题。它适用于大多数设备,但不适用于其他设备。\n我有:\n1。添加了一个扩展 FileProvider 的类:
\n\npublic class GenericFileProvider extends FileProvider {}\nRun Code Online (Sandbox Code Playgroud)\n\npublic class GenericFileProvider extends FileProvider {}\nRun Code Online (Sandbox Code Playgroud)\r\n <provider\r\n android:name=".Utils.GenericFileProvider"\r\n android:authorities="${applicationId}.provider"\r\n android:exported="false"\r\n android:grantUriPermissions="true">\r\n <meta-data\r\n android:name="android.support.FILE_PROVIDER_PATHS"\r\n android:resource="@xml/provider_paths"/>\r\n </provider>Run Code Online (Sandbox Code Playgroud)\r\n我的共享图像功能如下:
\n\n private fun shareInEmail() {\n val filename = "Avoir+$timeStamp.jpg"\n val filelocation = File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename)\n val path : Uri = FileProvider.getUriForFile(\n requireContext(),\n context!!.applicationContext.packageName.toString() + ".provider",\n filelocation\n )\n\n context!!.grantUriPermission(\n …Run Code Online (Sandbox Code Playgroud) 我已经阅读了http://developer.android.com/guide/components/tasks-and-back-stack.html,我已查阅了相关文档FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET,但我仍然不明白为什么谷歌的员工决定将其包括在内它在他们关于分享的博客文章中.
http://android-developers.blogspot.com/2012/02/share-with-intents.html
这是他们的代码片段:
Intent intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide what to do with it.
intent.putExtra(Intent.EXTRA_SUBJECT, “Some Subject Line”);
intent.putExtra(Intent.EXTRA_TEXT, “Body of the message, woot!”);
Run Code Online (Sandbox Code Playgroud)
文件说:
If set, this marks a point in the task's activity stack that should be cleared when the task is reset...
Run Code Online (Sandbox Code Playgroud)
我仍然不确定这面旗帜.我不确定我是否可以考虑将其包含在我的share()方法中,但如果Google在博客文章中使用它,那么我确信他们知道这种情况会发挥作用.提前致谢.
我开发了一个Android应用程序.我想把这个应用程序按钮放在facebook和twitter上,我想在点击按钮时分享我的android应用程序.我能怎么做?
谢谢.
我在使用FileProvider共享图像时遇到了Exception.以下是我以前的代码.
{
ArrayList<Uri> files = new ArrayList<Uri>();
files.add(getImageUriFromCache(context,bitmap,fileName));
}
private void startSharingIntent(ArrayList<Uri> files,String caption){
Intent i=new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
i.setType("image/png");
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_SUBJECT, "my app share");
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
i.putExtra(Intent.EXTRA_TEXT,caption);
i.putExtra(Intent.EXTRA_TITLE,caption);
context.startActivity(Intent.createChooser(i, "Tital"));
}
public Uri getImageUriFromCache(Context inContext, Bitmap inImage , String fileId) throws IOException{
File cachePath = new File(inContext.getFilesDir() , "images/");
cachePath.mkdirs(); // don't forget to make the directory
File newFile = new File(cachePath, "im"+fileId+"_post.png");
newFile.setReadable(true, false);
FileOutputStream stream = new FileOutputStream(newFile); // overwrites this image every time
inImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
Uri …Run Code Online (Sandbox Code Playgroud) android android-intent android-contentprovider android-sharing android-fileprovider
我试图让我自己的Chooser Activity来代替orroids share to ... popup.
我看了一下ChooserActivity扩展ResolverActivity并尝试复制代码.在我的清单中,我有
<intent-filter>
<action android:name="android.intent.action.CHOOSER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
而在onCreate我做
Intent intent = getIntent();
Parcelable parcelable = intent.getParcelableExtra("android.intent.extra.INTENT");
Intent shareIntent = (Intent) parcelable;
shareIntent.setComponent(new ComponentName(packageName, activityName));
startActivity(shareIntent);
finish();
Run Code Online (Sandbox Code Playgroud)
这有效,但附加了文件的共享除外.在那里,我得到了这个崩溃:
java.lang.SecurityException: UID 10098 does not have permission to content://com.android.chrome.FileProvider/images/screenshot/15305419271134395322470190280016.jpg [user 0]
at android.os.Parcel.readException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1888)
at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4365)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1610)
at android.app.Activity.startActivityForResult(Activity.java:4472)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at android.app.Activity.startActivityForResult(Activity.java:4430)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:741)
at android.app.Activity.startActivity(Activity.java:4791)
at android.app.Activity.startActivity(Activity.java:4759)
Run Code Online (Sandbox Code Playgroud)
看起来我的活动无法执行原始意图,因为它没有这些文件的权限.可以做些什么来解决这个问题?有办法获得许可吗?毕竟,android也以某种方式做到了.
我已经尝试了一些修复,但到目前为止还没有任何工作.如果我不能使它工作,我想从android调用通常的共享菜单来处理这个,但我也无法做到这一点.
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
Run Code Online (Sandbox Code Playgroud)
不起作用,因为这些标志已存在于intent中.另一方面,删除它们将导致共享工作,但文件将不会被共享,它将为空.
startActivity(Intent.createChooser(shareIntent, "test");
Run Code Online (Sandbox Code Playgroud)
会抛出同样的异常.
startActivity(Intent.createChooser(intent, "test"));
Run Code Online (Sandbox Code Playgroud)
我试图通过为原始意图制作一个选择器来调用android共享菜单,但它只是给了我一个窗口,让我选择我的应用程序. …
我有一个应用程序,用户应该能够在其中共享一些文本。现在我想提供 Android 提供的纯文本默认共享选项。我使用以下代码执行此操作:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
sendIntent.setType("text/plain");
Intent chooser = Intent.createChooser(sendIntent, "Share");
startActivity(chooser);
Run Code Online (Sandbox Code Playgroud)
这看起来有点像这样:
来源: http: //developer.android.com/training/basics/intents/sending.html
但现在我还希望能够在共享服务选择器对话框中多一个选项,该选项可以在我自己的代码中触发自定义操作。也就是说,我希望用户能够收藏某个条目。因此,除了通过短信、电子邮件、FB 等方式分享之外,我希望在该列表的顶部再添加一项,注明“添加到收藏夹”(如果可能的话,包括一个图标)。
所以我的问题是这是否可能?!?如果,如何:)
任何提示表示赞赏!
android ×10
android-sharing ×10
facebook ×2
twitter ×2
back-stack ×1
instagram ×1
share ×1
uri ×1