Android : Share drawable resource with other apps

徘徊边缘 提交于 2019-12-25 07:04:29

问题


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.

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

回答1:


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. 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();
    }
}



回答2:


You have to startActivity using intent of type Intent.ACTION_VIEW-

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



回答3:


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);


来源:https://stackoverflow.com/questions/36211474/android-share-drawable-resource-with-other-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!