我有一个运行良好的自定义 ListView ,现在我想共享列表中的图像和文本。我已经找到了如何从 SO 执行此操作的步骤,但是当我单击“共享”按钮时图像始终为空。
ImageView 使用 Glide 加载图像。
if (!Patterns.WEB_URL.matcher(Limage).matches()) {
viewholder.iview.setVisibility(View.GONE);
} else {
Glide.with(convertView.getContext()).load(Limage).centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// viewholder.progress.setVisibility(View.GONE);
return false;
}
}).into(viewholder.iview);
viewholder.iview.setVisibility(View.VISIBLE);
}
我创建了一个共享按钮,并在 onclick 中传递了以下代码。
viewholder.share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri bmpUri = getLocalBitmapUri(viewholder.iview);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
listdisplay.startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
}
});
要从 Imageview 获取图像,我使用以下代码。
private Uri getLocalBitmapUri(ImageView iview) {
Drawable drawable = iview.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) iview.getDrawable()).getBitmap();
Log.e("Shiva","Came inside drawable");
} else {
Log.e("Shiva","drawable is null"+drawable);
return null;
}
Uri bmpUri = null;
File file = new File(listdisplay.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
// **Warning:** This will fail for API >= 24, use a FileProvider as shown below instead.
return bmpUri;
}
所以,现在发生的事情是在检查“drawable instanceof BitmapDrawable”是否总是返回 null 的 if 步骤中。这里有什么问题吗? 注意:以上代码在适配器内部。
最佳答案
你可以使用这个加载图像:
Glide.with(this)
.load("https://cdn-images-1.medium.com/max/1200/1*hcfIq_37pabmAOnw3rhvGA.png")
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Log.d("Size ", "width :"+resource.getWidth() + " height :"+resource.getHeight());
imageView.setImageBitmap(resource);
storeImage(resource);
}
});
并将位图存储到外部存储,然后共享。
private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d(TAG,
"Error creating media file, check storage permissions: ");// e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
Log.d(TAG, "img dir: " + pictureFile);
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getApplicationContext().getPackageName()
+ "/Files");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
File mediaFile;
Random generator = new Random();
int n = 1000;
n = generator.nextInt(n);
String mImageName = "Image-"+ n +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
关于android - 使用 Intent 与 Glide 库共享图像 - 图像始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43042531/