Android:与其他应用共享可绘制资源

时间:2016-03-24 23:05:36

标签: android android-intent imageview drawable

在我的活动中,我有一个ImageView。我想,当用户点击它时,会打开一个对话框(如意图对话框),显示可以打开图像的应用列表,而不是用户可以选择应用并使用该应用显示图像。

我的活动代码:

@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()

3 个答案:

答案 0 :(得分:2)

您无法将位图传递给意图。

从我看到你想要从你的资源中分享一个drawable。首先,您必须将drawable转换为位图。然后你必须将位图作为文件保存到外部存储器,然后使用Uri.fromFile(新文件(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();
    }
}

答案 1 :(得分:-1)

您必须startActivity使用Intent.ACTION_VIEW -

类型的意图
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(<your_image_uri>, "image/*");
            startActivity(intent); 

答案 2 :(得分:-1)

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);
相关问题