gpt4 book ai didi

java - Android:从服务将字节转换为位图数据为空

转载 作者:行者123 更新时间:2023-12-02 12:17:19 27 4
gpt4 key购买 nike

服务提供 Json 数据

result = {"data":[{"image":"System.Byte[]"},{"image":"System.Byte[]"}]}

我的代码

JSONArray datas = new JSONArray(result.getString("data"));
for (int i=0;i<datas.length();i++){
JSONObject obj = items.getJSONObject(i);
byte[] blob=obj.getString("image").getBytes();
Bitmap bmp= BitmapFactory.decodeByteArray(blob,0,blob.length); // bmp is null
}

我能做什么?

最佳答案

// use this Code, this will work, if the Image string from server contain Base64 string
public void setImage(String theBase64String) {

String myTempFolderPath = CommonUtils.SDCardBasePath;
// If the Image String is Base 64 String
if (theBase64String != null) {
byte[] image_data_bytes = Base64.decode(theBase64String, 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";
}


// Create File Name with Current Date Time
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 = myTempFolderPath + "/" + captchaFileName;

String imgPath = writeImageBytesOnSdCard(MainActivity.this, imageSaveFolderPath, Image_Data_str);

try {
Bitmap bitmapSquare = createFixSizeBitmap(imgPath, 600, 600);
// Set image to ImagevIew
myImageView.setImageURI(Uri.parse(imgPath));

// Create Bitmap Image From Path.

} catch (Exception e) {
}
}
}
public Bitmap createFixSizeBitmap(String theTargetFile, int theReqHight, int theReqWidth) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(theTargetFile, bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap, theReqWidth, theReqHight, true);
return bitmap;
}
public String writeImageBytesOnSdCard(Activity theActivity, String theCustomImagePath, String theCustomImageData) {
String imagePath = theCustomImagePath;
String temp[] = theCustomImageData.split("\\,");
if (temp[0].contains("data:image/png")) {
imagePath = CommonUtils.getImagePath(imagePath, "png");
} else {
imagePath = CommonUtils.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 void updateMedia(final Activity theActivity, String filepath) {
MediaScannerConnection.scanFile(theActivity.getApplicationContext(), new String[]{filepath}, null, null);

}

关于java - Android:从服务将字节转换为位图数据为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46066527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com