有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java显示存储在内部存储器上的映像

我正在开发一个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) 个答案

  1. # 1 楼答案

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

    openFileOutput/openFileInput应始终成对使用