Imageview在第一次尝试时不显示图像

时间:2016-08-04 14:10:25

标签: android bitmap imageview orientation scaling

我做了一项活动,其中固定尺寸的图像视图显示来自自定义目的地的图片。缩放和方向已经有效,但是当我启动应用程序时,imageview首先显示一个小灰框。然而,当我再次加载它时,它确实显示了它应该的图片。谁能告诉我这里发生了什么以及我将如何解决它?我已经搜索了问题,但我认为它与我的代码执行的顺序有关。 这是我显示图像的代码:

public void refreshImageView(String highlight){
    File image;
    if(highlight==null){
        image=new File(mydb.getCityById(cityId).getPicture());
    }else{
        try{
        image=new File(mydb.getHighlightById(highlightId).getFile());
        }catch(Exception e){
            Log.i(TAG, "There is no file saved for this highlight");
            image=new File("");
        }
    }
    if(image.exists()&&image.toString().endsWith(".jpg")){
        mImagePreview.setVisibility(View.VISIBLE);

        int maxWidth=mImagePreview.getWidth(), maxHeight=mImagePreview.getHeight();
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        BitmapFactory.decodeFile(image.getAbsolutePath(),options);
        options.inSampleSize=calculateInSampleSize(options,maxWidth,maxHeight);
        options.inJustDecodeBounds=false;
        Bitmap mBitmap=BitmapFactory.decodeFile(image.getAbsolutePath(), options);
        try{
            mBitmap=checkOrientation(image.getAbsolutePath(),mBitmap);
        }catch(Exception e){
            Log.i(TAG, "Picture could not be rotated");
        }
        Bitmap bitmapPrev=Bitmap.createBitmap(
                mBitmap.getWidth()+20,
                mBitmap.getHeight()+20,Bitmap.Config.ARGB_8888);

        Canvas canvas=new Canvas(bitmapPrev);
        canvas.drawColor(R.color.black);
        canvas.drawBitmap(mBitmap, 10,10,new Paint(Paint.FILTER_BITMAP_FLAG));
        mImagePreview.setImageBitmap(bitmapPrev);
    }
    else{
        mImagePreview.setVisibility(View.GONE);
    }
}

//method for calculating sample size
private static int calculateInSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight){
    final int height=options.outHeight,width=options.outWidth;
    int inSampleSize=1;

    if(height>maxHeight || width>maxWidth){
        final int heightRatio=Math.round((float)height/(float)maxHeight),
                widthRatio=Math.round((float)width/(float)maxWidth);
        inSampleSize=heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    return inSampleSize;
}

//method to check orientation of picture
private Bitmap checkOrientation(String path, Bitmap bitmap) throws IOException{
    ExifInterface ei = new ExifInterface(path);
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    switch(orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return rotateImage(bitmap, 90);
        case ExifInterface.ORIENTATION_ROTATE_180:
            return rotateImage(bitmap, 180);
        case ExifInterface.ORIENTATION_ROTATE_270:
            return rotateImage(bitmap, 270);
        default:
            return bitmap;
    }
}

//method to rotate image
public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    Bitmap rotated = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    return rotated;

1 个答案:

答案 0 :(得分:0)

为什么所有的代码?图像视图可以处理所有加载和缩放!

Uri uri = Uri.fromFile(file);
imageView.setScaleType(...FIT_XY);
imageView.setImageURI(uri);