Share image to another app using Intent in android

回眸只為那壹抹淺笑 提交于 2019-11-29 16:56:08

try this

private class myTask extends AsyncTask<Void, Void, Bitmap> {


    protected Bitmap doInBackground(Void... params) {
        Bitmap myBitmap=null;
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);

        } catch (IOException e) {
            // Log exception
        }
        return myBitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        //do stuff

    }
}

 Bitmap returned_bitmap = new myTask().execute().get()



Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "download this image");
String bitmapPath = Images.Media.insertImage(getContentResolver(), returned_bitmap,"title", null);
Uri bitmapUri = Uri.parse(bitmapPath);    
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));

The documentation for EXTRA_STREAM says that the Uri needs to have a content scheme. file usually also works, at least on Android 6.0 and older. Few apps will expect an http URL.

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