1

我正在开发一个 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);
    }
}
4

1 回答 1

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

openFileOutput/openFileInput应始终成对使用

于 2013-07-12T09:35:15.893 回答