Android 无法在ImageView上设置位图不显示任何内容
我试图将图像放入ImageView中,但我做不到。当方法完成时,它不会在ImageView中显示任何内容,因为Android 无法在ImageView上设置位图不显示任何内容,android,bitmap,Android,Bitmap,我试图将图像放入ImageView中,但我做不到。当方法完成时,它不会在ImageView中显示任何内容,因为loadimage返回null。为什么会这样 代码如下: private void setSign() { File[] files = ImagesUtilities.getImages(directory, id); for (int i = 0; i < files.length; i++) { if (!files[
loadimage
返回null。为什么会这样
代码如下:
private void setSign() {
File[] files = ImagesUtilities.getImages(directory, id);
for (int i = 0; i < files.length; i++) {
if (!files[i].getName().contains("firma")) {
signFile = files[i].getAbsolutePath();
}
}
if(new File(signFile).exists()){
sign.setImageBitmap(loadImage(signFile));
sign.invalidate();
}
}
private Bitmap loadImage(String imgPath) {
BitmapFactory.Options options;
try {
options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void setSign(){
File[]files=ImagesUtilities.getImages(目录,id);
对于(int i=0;i
最后一次尝试中signFile的值为:/storage/emulated/0/Pictures/181/20140414153624/firma357840194.jpg
if(new File(signFile).exists()){
sign.setImageBitmap(BitmapSize.getDecodedBitmap(path, 400, 400););
sign.invalidate();
}
班级:::
public class BitmapSize{
public static Bitmap getDecodedBitmap(String path, float target_width, float target_height) {
Bitmap outBitmap = null;
try {
Options decode_options = new Options();
decode_options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path,decode_options); //This will just fill the output parameters
int inSampleSize = calculateInSampleSize(decode_options, target_width, target_height);
Options outOptions = new Options();
outOptions.inJustDecodeBounds = false;
outOptions.inSampleSize = inSampleSize;
outOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
outOptions.inScaled = false;
Bitmap decodedBitmap = BitmapFactory.decodeFile(path,outOptions);
outBitmap = Bitmap.createScaledBitmap(decodedBitmap,// (int)target_width, (int)target_height, true);
(int)((float)decodedBitmap.getWidth() / inSampleSize),
(int)((float)decodedBitmap.getHeight() / inSampleSize), true);
System.out.println("Decoded Bitmap: Width " + outBitmap.getWidth() + " Height = " + outBitmap.getHeight() + " inSampleSize = " + inSampleSize);
} catch (Exception e) {
// TODO: handle exception
}
return outBitmap;
}
public static int calculateInSampleSize(Options options, float reqWidth, float reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
公共类位图大小{
公共静态位图getDecodedBitmap(字符串路径、浮动目标宽度、浮动目标高度){
位图输出位图=空;
试一试{
选项解码_选项=新选项();
decode_options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(路径,decode_选项);//这将只填充输出参数
int inSampleSize=计算样本大小(解码选项、目标宽度、目标高度);
选项输出选项=新选项();
outOptions.inJustDecodeBounds=false;
outOptions.inSampleSize=inSampleSize;
outOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
outOptions.inScaled=false;
位图decodedBitmap=BitmapFactory.decodeFile(路径,输出选项);
outBitmap=Bitmap.createScaledBitmap(decodedBitmap,//(int)目标宽度,(int)目标高度,true);
(int)((float)decodedBitmap.getWidth()/inSampleSize),
(int)((float)decodedBitmap.getHeight()/inSampleSize),true);
System.out.println(“解码位图:宽度”+outBitmap.getWidth()+“高度=“+outBitmap.getHeight()+”inSampleSize=“+inSampleSize”);
}捕获(例外e){
//TODO:处理异常
}
返回位图;
}
公共静态int-calculateInSampleSize(选项、浮点请求宽度、浮点请求高度){
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
最终整数高度比=数学圆((浮动)高度
/(浮动)高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
}
是的,loadImage return null,我现在可以检查,但是为什么?如果文件存在..:你有任何异常现在,只有loadImage return null,但没有任何异常。唯一的方法是解码引发异常。你的图像有多大?它可能太大并导致异常。