Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

android - Download image using Glide And bitmap And Share

i want to share image which is taken from my website using json, i need to download image and than share , currently i am using this method but its not working , i am not familiar with Glide and Bitmap, Please Help

here is my code :

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

                    }
                });

And Bitmap code

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


    }
}

share Intent

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

and Bitmap load image

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

    }
}
question from:https://stackoverflow.com/questions/65885845/download-image-using-glide-and-bitmap-and-share

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have a problem to load bitmap try this (it works)

For other problems comment this

private void loadBitmapByGlide(String imageUrl) {
        Glide.with(this)
                .asBitmap()
                .load(imageUrl)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap image, @Nullable Transition<? super Bitmap> transition) {
                        //TODO save, share image or something else 
                        Toast.makeText(getApplicationContext(), "Image loaded: " + image.getWidth() + "-" + image.getHeight(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...