0

I'm trying to display ImageView using Bitmap and Bitmap get it's value from sharedpref path

 /// Activity Fields

   ٍString mWinPhotoPath, mLosePhotoPath;
    ImageView winnerImage, loserImage;
    Bitmap winImage, loseImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_winner);
//// Get shared pref values
        mWinPhotoPath = **sharedPreferences.getString(getString(R.string.sharedPreferences_winner_pic), getString(R.string.error_sorry_message));  // win pic
        mLosePhotoPath = sharedPreferences.getString(getString(R.string.sharedPreferences_loser_pic), getString(R.string.error_sorry_message)); // lose pic**


 **winImage = BitmapFactory.decodeFile(mWinPhotoPath);
loseImage = BitmapFactory.decodeFile(mLosePhotoPath);**


// Activity Objects

        **winnerImage = findViewById(R.id.winner_image);**
        **loserImage = findViewById(R.id.loser_image);**

// set values


          **winnerImage.setImageBitmap(winImage);**
          **loserImage.setImageBitmap(loseImage);**

I don't have any error in my log but the problem is the ImageView didn't display Bitmap also I checked that the shared pref get the path value correctly

Is there any logical error in my code !!? What's the problem ?

beso
  • 9
  • 4

2 Answers2

0

To get bitmap from a path you need to use this code below

File image = new File(mWinPhotoPath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
winImage = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
  • why I have to write your code !! that's to much I already have the path any way I tried your code but it have more error (imageName-parent) can u suggest another one – beso Jul 24 '18 at 22:54
  • Take a look at the answer of this question. you have to get the file from the path. https://stackoverflow.com/questions/4181774/show-image-view-from-file-path – Nishikanto Sarkar Simul Jul 25 '18 at 05:42
0

If you are storing path of file in SharedPrefrence then you can decode file into bitmap by using its path.

        File f = new File(file path....);
        Bitmap map = BitmapFactory.decodeFile(f.getAbsolutePath());
        image.setImageBitmap(map);

OR

            String fileName = "...."; // file path
            File completeFile = new File(fileName);
            FileInputStream readPicture = new FileInputStream(completeFile);
            BufferedInputStream bf = new BufferedInputStream(readPicture);
            Bitmap bitmap = BitmapFactory.decodeStream(bf);
Shahzad Afridi
  • 2,058
  • 26
  • 24
  • the same problem that's what i did File f = new File(mWinPhotoPath); winImage = BitmapFactory.decodeFile(f.getAbsolutePath()); winnerImage.setImageBitmap(winImage); – beso Jul 24 '18 at 22:49