[英]Android : Share drawable resource with other apps
In my activity I have an ImageView. 在我的活动中,我有一个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. 我想要,当用户单击它时,会打开一个对话框(如意图对话框),该对话框显示可以打开图像的应用程序列表,而不是用户可以选择一个应用程序并显示该应用程序的图像。
my activity code : 我的活动代码:
@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. 您不能将位图传递给意图。
From what I see you want to share a drawable from your resources. 从我的角度来看,您希望共享资源中的可绘制对象。 So first you have to convert the drawable to a bitmap. 因此,首先您必须将drawable转换为位图。 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. 然后,您必须将位图作为文件保存到外部存储器,然后使用Uri.fromFile(new File(pathToTheSavedPicture))获取该文件的uri,并将该uri传递给这样的意图。
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();
}
}
You have to startActivity
using intent of type Intent.ACTION_VIEW
- 您必须使用Intent.ACTION_VIEW
类型的Intent.ACTION_VIEW
startActivity
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(<your_image_uri>, "image/*");
startActivity(intent);
Create a chooser by using the following code. You can add it in the part where you say imageview.setonclicklistener().
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.