使用滑动和位图下载图像和共享



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

下面是我的代码:
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();
}
}

如果你在加载位图时遇到问题,试试这个(它可以工作)

其他问题请注释

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

最新更新