如何从 android imageview 中的 dataurl 加载图像?
How to load an image from dataurl in android imageview?
我有以下数据网址
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARgAAADSCAMAAACmVC/YAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6ODMwQTFBMjFDRkYxMTFFNDk1QzNFODQ3NkZGQzk2MEUiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6ODMwQTFBMjJDRkYxMTFFNDk1QzNFODQ3NkZGQzk2MEUiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo4MzBBMUExRkNGRjExMUU0OTVDM0U4NDc2RkZDO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gwOMowaTeLRv+9rYVbChRbSeBr1F0Hm9DU5RWSC+RFO5Lq2DuZPpryvS5hFknGawxyezF+xpGU3mZUSvD6oVFS/1kSvLGnAtJwL96UqTadyIqC8Wx/PRPLKmbloOrGpzzQCQWRB1vvvZJPMxNkR1kucvGk5YJaxwjM8fJ9YbhPn0ybzFTK+vZ/ZxjSR0Wk4L1UU5hpep6g86xetNuuira4Iky6vo9aSrS9WXDXcuasIQ63RG23c60mTwo3Hy01hHvZqE1myTvIS/yXXuC6DYRWivMTstbEzOSV9LQW2rqcl0Hc6yd/U2rjvF/gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH7IdixtjXannItg1w8WxhBniq59rIr/I3OmwiLGHraC/hW/a2s/n+ixg419b8AHyCGES4X6WNZom+2anff7uZwxooP4EQq6XK7irn8aqDIE+yInN3Z58t+v8cdEd7J/7X2wvs5JfyN7Vyfrv9Nmgl2RDHIEKOpCAhAMmj5+KJBGjCC26IPoIhCMBqiKhPFjRHI4BmwmMRgNmyENRiwMNkMZzHadkgGbCQzmSwyfz0PewRvMMx2+jshUJT2YY+jokHqIDcalQn14sspk7ZAaYTPBECmwlA02c9WbKAcjYTPzSJ2z6pPjn2sz00GS5F3O5+jMAAAAAAAAAAAAAAAAAAAAAADAL/n7nOs09bYCYvG3+WlXLNtCzWNnf0HspptVEEu08HuUhcCZy7tyeHRNnuhkhfSW3etH3vF4rP6ghFHJJW/JPeuNMhMX21ErX8q3exMXRXawqIi+KzJThu/FDW4LdZNk8/u9/OWXYmds1qWNj8aWd/75qxC1
有没有办法在 imageview 中加载 dataUrl?
只需使用 Base64.decode() method to receive a byte array, and then create the Bitmap
using BitmapFactory.decodeByteArray() 方法解码 Base64 图像编码。
首先你需要convert the Base64 encoded string到位图。
以下链接问题的代码片段:
byte[] decodedString = Base64.decode("Your Base64 String", Base64.DEFAULT);
Bitmap bitMap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
完成后,您的位图就在 bitmap
中了
假设您的 imageView
定义为 R.id.image_view
然后就像处理任何其他位图一样:
ImageView imageView;
imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
如果您将输入的 Base64 部分取出,可以通过以下方式轻松将其转换为位图:
byte[] decodedString = Base64.decode("your base64 string", Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
在字符串变量解码中获取base64字符串
String encodedImage;// 在此字符串中设置 base64 字符串值
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
位图 decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
因为我收到错误:
java.lang.IllegalArgumentException: bad base-64
,我使用了以下解决方法:
import java.io.ByteArrayInputStream
import java.io.InputStream
import android.util.Base64
...
val imageData: ByteArray = Base64.decode(dataUri.substring(dataUri.indexOf(",") + 1), Base64.DEFAULT)
val inputStream: InputStream = ByteArrayInputStream(imageData)
val bitmap: Bitmap = BitmapFactory.decodeStream(inputStream)
imageViewComponent.setImageBitmap(bitmap)
也许它会对某人有所帮助。
我有以下数据网址
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARgAAADSCAMAAACmVC/YAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIge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MRgNmyENRiwMNkMZzHadkgGbCQzmSwyfz0PewRvMMx2+jshUJT2YY+jokHqIDcalQn14sspk7ZAaYTPBECmwlA02c9WbKAcjYTPzSJ2z6pPjn2sz00GS5F3O5+jMAAAAAAAAAAAAAAAAAAAAAADAL/n7nOs09bYCYvG3+WlXLNtCzWNnf0HspptVEEu08HuUhcCZy7tyeHRNnuhkhfSW3etH3vF4rP6ghFHJJW/JPeuNMhMX21ErX8q3exMXRXawqIi+KzJThu/FDW4LdZNk8/u9/OWXYmds1qWNj8aWd/75qxC1
有没有办法在 imageview 中加载 dataUrl?
只需使用 Base64.decode() method to receive a byte array, and then create the Bitmap
using BitmapFactory.decodeByteArray() 方法解码 Base64 图像编码。
首先你需要convert the Base64 encoded string到位图。 以下链接问题的代码片段:
byte[] decodedString = Base64.decode("Your Base64 String", Base64.DEFAULT);
Bitmap bitMap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
完成后,您的位图就在 bitmap
中了
假设您的 imageView
定义为 R.id.image_view
然后就像处理任何其他位图一样:
ImageView imageView;
imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
如果您将输入的 Base64 部分取出,可以通过以下方式轻松将其转换为位图:
byte[] decodedString = Base64.decode("your base64 string", Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
在字符串变量解码中获取base64字符串
String encodedImage;// 在此字符串中设置 base64 字符串值 byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT); 位图 decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
因为我收到错误:
java.lang.IllegalArgumentException: bad base-64
,我使用了以下解决方法:
import java.io.ByteArrayInputStream
import java.io.InputStream
import android.util.Base64
...
val imageData: ByteArray = Base64.decode(dataUri.substring(dataUri.indexOf(",") + 1), Base64.DEFAULT)
val inputStream: InputStream = ByteArrayInputStream(imageData)
val bitmap: Bitmap = BitmapFactory.decodeStream(inputStream)
imageViewComponent.setImageBitmap(bitmap)
也许它会对某人有所帮助。