[英]How to get URI on imageview with Glide
我正在使用“Glide”將圖像從服務器加載到 ImageView。 我想知道是否可以從 imageview 本身中提取該 URI。
ImageView contentImage = (ImageView) findViewById(R.id.contentImage);
…………
Glide.with(getApplicationContext()).load(contentImageUrl)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(contentImage);
我的目標是在 Imageview 中共享圖像。 如何使用 Glide 在 imageview 上獲取 URI?
Uri uri = ??????????????
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
要讓 Uri 將 ur Glide
實現更改為:
Bitmap bitmap;
Glide.with(getApplicationContext())
.load(contentImageUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
// you can do something with loaded bitmap here
contentImage.setImageBitmap(resource);
bitmap= resource;
}
});
現在調用該方法;
//get URI from bitmap
Uri uri = getImageUri(getApplicationContext(),bitmap);
在getImageUri
函數中:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), inImage, UUID.randomUUID().toString() + ".png", "drawing");
return Uri.parse(path);
}
您需要在清單中添加權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
也用於運行時權限:Api>=23
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
addNewPostImage 是圖像視圖; 圖像插入了 Glide 庫
Imageview addNewPostImage = findviewbyid(R.id.addNewPostImage);
Bitmap bitmap;
EditText addNewPostWebLink = findviewbyid(R.id.addNewPostWebLink);
private void getUri() {
String imageUrl = addNewPostWebLink.getText().toString().trim();
Glide.with(getApplicationContext())
.asBitmap()
.load(imageUrl)
//.fitCenter()
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
// you can do something with loaded bitmap here
//bitmap = resource;
addNewPostImage.setImageBitmap(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
postImageUri = getImageUri(NewPostActivity.this);
Toast.makeText(this, Objects.requireNonNull(postImageUri).toString(), Toast.LENGTH_SHORT).show();
}
由於圖像位圖已在資源主體上設置,我們調用 getImageUri(),它首先從圖像中獲取位圖,對其進行壓縮並存儲圖像。 存儲后,它返回存儲的路徑。 圖像存儲在外部存儲中
private Uri getImageUri(NewPostActivity newPostActivity) {
bitmap = ((BitmapDrawable) addNewPostImage.getDrawable()).getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(newPostActivity.getContentResolver(), bitmap, UUID.randomUUID().toString() + ".png", "drawing");
return Uri.parse(path);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.