我正在开发一个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);
}
}
答案 0 :(得分:3)
FileInputStream fIn = openFileInput(Constants.FB_PROFILE_IMAGE_FILE_NAME);
Bitmap myBitmap = BitmapFactory.decodeStream(fIn);
openFileOutput/openFileInput
应始终用于配对