Android 将字符串转换为图像/tiff
我需要将base64代码的TextView转换为图像Android 将字符串转换为图像/tiff,android,Android,我需要将base64代码的TextView转换为图像 TextView tv = (TextView)findViewById(R.id.textView); tv.buildDrawingCache(); ImageView img = (ImageView)findViewById(R.id.imageView); img.setImageBitmap(tv.getDrawingCache()); 这不起作用:/ 有人能帮我吗?这是一个示例(假设您的文本是正确的): 我看不到base64。
TextView tv = (TextView)findViewById(R.id.textView);
tv.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.imageView);
img.setImageBitmap(tv.getDrawingCache());
这不起作用:/
有人能帮我吗?这是一个示例(假设您的文本是正确的):
我看不到base64。代码在TV64中。如果您不提供base64字符串,我们当然无法提供帮助。为什么要将字符串放在文本视图中?这有什么意义?这些角色看起来好看吗?准备好了,检查一下变化。在代码中定义字符串并将其分配给要开始的textview。目前与textview没有连接。
TextView tv = (TextView)findViewById(R.id.textView);
ImageView img = (ImageView)findViewById(R.id.imageView);
String base64String = tv.getText();
String base64Image = base64String.split(",")[1];
byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
img.setImageBitmap(decodedByte);
// add dependency in gradle
compile 'com.github.bumptech.glide:glide:3.7.0'
if (theCaptchaBase64 != null) {
byte[] image_data_bytes = Base64.decode(theCaptchaBase64, Base64.NO_WRAP);
String Image_Data_str = null;
try {
Image_Data_str = new String(image_data_bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String imageFileExt = ".jpg";
if (Image_Data_str != null && Image_Data_str.contains("png")) {
imageFileExt = ".png";
}
long msTime = System.currentTimeMillis();
Date dt = new Date(msTime);
String format = "yyMMddHHmmssSSS";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
String captchaFileName = String.format("Captcha" + "_%s", sdf.format(dt).toString());
//String captchaFileName = "Captcha";
captchaFileName = captchaFileName + imageFileExt;
String imageSaveFolderPath = TempFolderPath + "/" + captchaFileName;
String imgPath = writeImageBytesOnSdCard(this, imageSaveFolderPath, Image_Data_str);
loadImageWithGlide(this,ImageViiew,imgPath);
}
public static void loadImageWithGlide(Context theCtx, ImageView theImageView, String theUrl) {
// skipMemoryCache, because it load the same image because URL not change.
Glide.with(theCtx)
.load(theUrl)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(theImageView);
//Glide.with(mContext).load(imagePath).into(myHolder.mImageViewUserImage);
}
public static String writeImageBytesOnSdCard(Activity theActivity, String theCustomImagePath, String theCustomImageData) {
String imagePath = theCustomImagePath;
String temp[] = theCustomImageData.split("\\,");
if (temp[0].contains("data:image/png")) {
imagePath = getImagePath(imagePath, "png");
} else {
imagePath = getImagePath(imagePath, "jpg");
}
byte[] data = null;
try {
//data=myImageData1.getBytes("UTF-8");
data = Base64.decode(temp[1], Base64.DEFAULT);
FileOutputStream outputStream = null;
outputStream = new FileOutputStream(new File(imagePath), false);
InputStream in = new ByteArrayInputStream(data);
OutputStream out = new BufferedOutputStream(outputStream);
byte[] loginBuffer = new byte[1024];
int byteRead = 0;
while ((byteRead = in.read(loginBuffer)) != -1) {
out.write(loginBuffer, 0, byteRead);
}
out.close();
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// code for sending a notification to the media scanner
updateMedia(theActivity, imagePath);
} catch (FileNotFoundException e) {
imagePath = "";
e.printStackTrace();
} catch (IOException e) {
imagePath = "";
e.printStackTrace();
}
return imagePath;
}
public static String getImagePath(String theExportImagePath, String theRequiredExt) {
File f = new File(theExportImagePath);
String fileName = f.getName();
String parent = f.getParent();
String filename_Without_Ext = fileName;
int dotposition = fileName.lastIndexOf(".");
if (dotposition != -1)
filename_Without_Ext = fileName.substring(0, dotposition);
return parent + "/" + filename_Without_Ext + "." + theRequiredExt;
}
public static void updateMedia(final Activity theActivity, String filepath) {
MediaScannerConnection.scanFile(theActivity.getApplicationContext(), new String[]{filepath}, null, null);
}