在这里,我想从字符串URL转换图像。尽管有一个包含图像的URL,但它返回null。我在下面共享了代码。
private byte[] convertImageToByteArray(String imgPath)
{
byte[] byteArray = null;
Bitmap bmp = BitmapFactory.decodeFile(imgPath);
if(bmp != null)
{
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
try
{
stream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
try {
Bitmap bmpDefault = BitmapFactory.decodeResource(getResources(), R.drawable.na);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bmpDefault.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bmpDefault.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return byteArray;
}
控制流不执行if块,而是进入else块,并且BitmapFactory.decodeFile()始终返回null。我哪里出问题了?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句