title: How to save new imagebutton picture and load newly created imagebutton image on relaunch
body: <p>I am trying to reload the image that I get from either my gallery or camera in the imagebutton once the app closes </p>
  
  <p>I have tried saving it to a file and loading from that file but I cannot get it to work.  </p>
  
  <pre><code>
  
  
  public class HomeFragment extends Fragment {
  
  
      ImageButton profilePic;
  
      Drawable myDrawable;
      Bitmap bitmap ;
  
  
  
  
      String photoPath = Environment.getExternalStorageDirectory().toString();
  
      public View onCreateView(@NonNull LayoutInflater inflater,
                               ViewGroup container, Bundle savedInstanceState) {
                 View root = inflater.inflate(R.layout.fragment_home, container, false);
  
  
  
         profilePic = (ImageButton) root.findViewById(R.id.imageButton);
          myDrawable = profilePic.getBackground();
          bitmap = drawableToBitmap(myDrawable);
  
  
  // set up listener on ImageButton to load method changeProfilePicture() when user clicks profilePic
          profilePic.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
  
                  changeProfilePicture();
              }
          });
  
  
          int Permission_All = 1;
  
          String[] Permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
          if(!hasPermissions(getActivity(), Permissions)){
              ActivityCompat.requestPermissions(getActivity(), Permissions, Permission_All);
  
          }
  
  
         loadImageFromStorage(photoPath);
          return root;
  
      }
  
  
      public void onResume() {
  
          super.onResume();
  
          loadImageFromStorage(photoPath);
  
      }
  
      public void onStart() {
  
          super.onStart();
  
      }
  
      public void onPause() {
  
          super.onPause();
  
  
      }
  
      public void onStop() {
  
          super.onStop();
  
          saveBitmap(bitmap);
      }
  
      public void onDestroy() {
  
          super.onDestroy();
  
  
      }
  
      static final int REQUEST_IMAGE_CAPTURE = 1;
      static final int REQUEST_GALLERY = 0;
  
      //create changeProfilePicture method and call it when the  ImageButton is pressed
      public void changeProfilePicture() {
  
  // add alert dialog to ask user how they would like to change their profile icon
  
  
  
      }
  
  
      // override onActivityResult to allow the imageButton to be changed to picture taken from camera or gallery
  
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if(resultCode == RESULT_OK) {
              //create a switch based on requestCode
              switch (requestCode) {
                  // if user clicks change profilePic through gallery use picture user picked from gallery
                  case REQUEST_GALLERY:
                      Uri galleryImage = data.getData();
                      try {
                          Bitmap galleryBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), galleryImage);
                          galleryBitmap = Bitmap.createScaledBitmap(galleryBitmap, 200, 200, false);
                          profilePic.setImageBitmap(galleryBitmap);
                         saveBitmap(galleryBitmap);
                      } catch (IOException e) {
                          Log.i("TAG", "Exception " + e);
                      }
  
                      break;
                  // if user clicks change profilePic through camera use picture user took from camera app
  
                  case REQUEST_IMAGE_CAPTURE:
  
                      Bundle extras = data.getExtras();
                      Bitmap imageBitmap = (Bitmap) extras.get("data");
                      imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 200, 200, false);
                      profilePic.setImageBitmap(imageBitmap);
                      saveBitmap(imageBitmap);
  
                      break;
              }
  
          }
      }
  
  
  
      private void saveBitmap(Bitmap bm){
          File file = Environment.getExternalStorageDirectory();
          File newFile = new File(file, "myImage.jpg");
  
          try {
              FileOutputStream fileOutputStream = new FileOutputStream(newFile);
              bm.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
              fileOutputStream.flush();
              fileOutputStream.close();
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          Log.i( "getExternalStorageDirectory", file.getPath()) ;
      }
  
      private void loadImageFromStorage(String path)
      {
  
          try {
              File f=new File(path, "myImage.jpg");
              Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
              profilePic.setImageBitmap(b);
          }
          catch (FileNotFoundException e)
          {
              e.printStackTrace();
          }
  
      }
  
      public static Bitmap drawableToBitmap (Drawable drawable) {
          Bitmap bitmap = null;
  
          if (drawable instanceof BitmapDrawable) {
              BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
              if(bitmapDrawable.getBitmap() != null) {
                  return bitmapDrawable.getBitmap();
              }
          }
  
          if(drawable.getIntrinsicWidth() &lt;= 0 || drawable.getIntrinsicHeight() &lt;= 0) {
              bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
          } else {
              bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
          }
  
          Canvas canvas = new Canvas(bitmap);
          drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
          drawable.draw(canvas);
          return bitmap;
      }
  
      public static boolean hasPermissions(Context context, String... permissions)
      {
  
          if(Build.VERSION.SDK_INT&gt;= Build.VERSION_CODES.M &amp;&amp; context!=null &amp;&amp; permissions!=null){
              for(String permission: permissions){
                  if(ActivityCompat.checkSelfPermission(context, permission)!= PackageManager.PERMISSION_GRANTED){
                      return  false;
                  }
              }
          }
          return true;
      }
  
  }
  
  
  
  // what I have for my ImageButton in xml
  
     &lt;ImageButton
          android:id="@+id/imageButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:contentDescription="@string/profilePic"
          android:onClick="changeProfilePicture"
          android:paddingLeft="0dp"
          android:paddingTop="0dp"
          android:paddingRight="0dp"
          android:paddingBottom="0dp"
          android:layout_marginTop="2dp"
          android:layout_marginBottom="5dp"
          android:background="#0000"
          app:srcCompat="@mipmap/ic_launcher_round" /&gt;
  
  </code></pre>
  
  <pre><code>I have the following permissions inside the manifest.xml 
  
   &lt;uses-feature
          android:name="android.hardware.camera"
          android:required="true" /&gt; 
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
  </code></pre>
  
  <p>I am trying to save the new ImageButton from my result and load it when the app is relaunched. Can this be done with an ImageButton? Can it be saved to and loaded from a file?</p>