Android : Share drawable resource with other apps(Android : 与其他应用共享可绘制资源)
问题描述
在我的活动中,我有一个 ImageView.我想,当用户点击它时,会打开一个对话框(如意图对话框),显示可以打开图像的应用程序列表,而不是用户可以选择一个应用程序并显示该应用程序的图像.
In my activity I have an ImageView. I want ,when user click on it, a dialog opens (like intent dialogs) that show list of apps which can open image than user can choose a app and show the image with that app.
我的活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView) findViewById(R.id.imageid);
iv.setImageResource(R.drawable.dish);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//here is where I want a dialog that I mentioned show
}
});
}// end onCreate()
推荐答案
您不能将位图传递给意图.
You can't pass a bitmap to an intent.
据我所知,您想从您的资源中共享一个可绘制对象.所以首先你必须将drawable转换为位图.然后您必须将位图作为文件保存到外部存储器,然后使用 Uri.fromFile(new File(pathToTheSavedPicture)) 获取该文件的 uri 并将该 uri 传递给这样的意图.
From what I see you want to share a drawable from your resources. So first you have to convert the drawable to a bitmap. And then You have to save the bitmap to the external memory as a file and then get a uri for that file using Uri.fromFile(new File(pathToTheSavedPicture)) and pass that uri to the intent like this.
shareDrawable(this, R.drawable.dish, "myfilename");
public void shareDrawable(Context context,int resourceId,String fileName) {
try {
//convert drawable resource to bitmap
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
//save bitmap to app cache folder
File outputFile = new File(context.getCacheDir(), fileName + ".png");
FileOutputStream outPutStream = new FileOutputStream(outputFile);
bitmap.compress(CompressFormat.PNG, 100, outPutStream);
outPutStream.flush();
outPutStream.close();
outputFile.setReadable(true, false);
//share file
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile));
shareIntent.setType("image/png");
context.startActivity(shareIntent);
}
catch (Exception e) { Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
}
}
这篇关于Android : 与其他应用共享可绘制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android : 与其他应用共享可绘制资源
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01