使用 Glide 和位图下载图像并分享

问题描述

我想分享使用 json 从我的网站上获取的图像,我需要下载图像然后分享,目前我正在使用这种方法但它不起作用,我不熟悉 Glide 和 Bitmap,请帮助

>

这是我的代码:

 Glide.with(context)
                .asBitmap()
                .load(imageUrl)
                .apply(new RequestOptions()
                        .placeholder(R.drawable.ic_placeholder)
                        .diskCacheStrategy(DiskCacheStrategy.DATA))

                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource,@Nullable Transition<? super Bitmap> transition) {

                        
                    }

                    public void onLoadFailed(@Nullable Drawable errorDrawable) {

                    }
                });

和位图代码

 private class ShareTask extends AsyncTask<Bitmap,Void,File> {
    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    @Override
    protected File doInBackground(Bitmap... params) {
        try {
            File cachePath = new File(context.getCacheDir(),AppConstants.TEMP_PATH);
            if (!cachePath.exists()) {
                cachePath.mkdirs();
            }
            FileOutputStream stream = new FileOutputStream(cachePath + "/" + AppConstants.TEMP_PNG_NAME);
            params[0].compress(Bitmap.CompressFormat.PNG,100,stream);
            stream.close();
            return cachePath;
        } catch (IOException ex) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(@Nullable File result) {
        // share wallpaper
        File newFile = new File(result,AppConstants.TEMP_PNG_NAME);
        Uri uri = FileProvider.getUriForFile(context,getContext().getResources().getString(R.string.fileprovider),newFile);
        launchShareIntent(uri);


    }
}

分享意图

 Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    shareIntent.setDataAndType(uri,mActivity.getContentResolver().getType(uri));
    shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
    shareIntent.setType(mActivity.getContentResolver().getType(uri));
    context.startActivity(Intent.createChooser(shareIntent,getContext().getResources().getString(R.string.share_wallpaper_title)));

和位图加载图像

  public void shareWallpaper() {
    if (mBitmap != null) {
        new ShareTask().execute(mBitmap);
    } else {

        Toast.makeText(context,getContext().getResources().getString(R.string.wallpaper_not_loaded),Toast.LENGTH_SHORT).show();

    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)