0

嗨,我从 URL 下载了一张图片,并将其作为位图写入文件。我正在尝试从文件中检索位图并显示它。有谁知道如何做到这一点?这是我尝试过的,它没有错误,但它不显示图像。

全球.java

public static void saveBitmapToFile(Context activityContext, Bitmap bitmap, String FileName){

    try{
        File file = new File(FileName);
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        fOut.flush();
        fOut.close();}
    catch (Exception e) {
        e.printStackTrace();
        Log.i(null, "Save file error!");

        }

    }

    public static Bitmap returnBitmapFromFile(Context activityContext, String FileName){
        Bitmap bitmap = BitmapFactory.decodeFile(FileName);
        return bitmap;

    }

GetBitmap.java

 public void downloadUserPhoto(){



            String userPhotoUrl = "http://static.bbci.co.uk/h4discoveryzone/ic/newsimg/media/images/229/129/68805000/jpg/_68805145_pahs2.jpg"

            userPhoto = Global.createBitmapFromUrl(this, userPhotoUrl); 
            Global.saveBitmapToFile(this, userPhoto, "user_photo");
}                       

 public void getUserPhoto(){
         loadingText.setText("Getting User Pictures..."); 
          Bitmap setUserPhoto = Global.returnBitmapFromFile(this, "user_photo");
         logo.setImageBitmap(setUserPhoto);


         }
4

1 回答 1

1

尝试使用以下命令打开它FileInputStream

public static Bitmap returnBitmapFromFile(Context activityContext, String FileName){
    FileInputStream in = new FileInputStream(FileName);
        BufferedInputStream buf = new BufferedInputStream(in);
        byte[] bitMapA= new byte[buf.available()];
        buf.read(bitMapA);
        buf.close();
        in.close();
        Bitmap bitmap = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length);
    return bitmap;

}

其他方法是使用openFileInput

public static Bitmap returnBitmapFromFile(Context activityContext, String FileName){

  FileInputStream fis;      

    try {
        fis = openFileInput(FileName);
        Bitmap bitmap = BitmapFactory.decodeStream(fis);
        fis.close();
                    return bitmap;
    } catch (FileNotFoundException e) {
                    e.printStackTrace();
                     return null;
    } catch (IOException e) {
        e.printStackTrace();
                    return null;
    }
}
于 2013-09-23T15:40:42.433 回答