显示存储在内部存储中的图像

时间:2013-07-12 09:33:30

标签: java android

我正在开发一个Android应用程序,我这样做是为了将位图保存到内部存储中:

    protected void onPostExecute(Bitmap profilePicture)
    {
        FileOutputStream fOut = null;

        try
        {
            fOut = openFileOutput(Constants.FB_PROFILE_IMAGE_FILE_NAME, Context.MODE_PRIVATE);
            profilePicture.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            fOut.flush();
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            try
            {
                fOut.close();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        loadingDialog.dismiss();
    }

如何打开它并将其展示到ImageView

此代码不起作用,因为imgFile不存在:

private void loadAndShowUserProfileImage()
{
    File imgFile = new  File(Constants.FB_PROFILE_IMAGE_FILE_NAME);
    if(imgFile.exists())
    {
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        userImageProfile.setImageBitmap(myBitmap);
    }
}

1 个答案:

答案 0 :(得分:3)

FileInputStream fIn = openFileInput(Constants.FB_PROFILE_IMAGE_FILE_NAME);
Bitmap myBitmap = BitmapFactory.decodeStream(fIn);

openFileOutput/openFileInput应始终用于配对