1

Once you have recovered an image of the user, how to save it in drawable in the app ? For use it in other activities.

Retrieve the image from the user's gallery and put it in ImageView :

public class Profil extends AppCompatActivity {

private Button btnImport;
public ImageView selectedImg;
static final int RESULT_LOAD_IMG = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profil);

    ImageView btn_supervisor = findViewById(R.id.btn_supervisor);
    ImageView btn_add = findViewById(R.id.btn_add);
    ImageView btn_profile = findViewById(R.id.btn_profile);

    btnImport = findViewById(R.id.modifie_button);
    selectedImg = findViewById(R.id.modifie_image);


    btnImport.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
        }
    });
}
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);



    if (resultCode == RESULT_OK) {
        try {
            final Uri imageUri = data.getData();
            final InputStream imageStream = getContentResolver().openInputStream(imageUri);
            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
            selectedImg.setImageBitmap(selectedImage);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Une erreur s'est produite",Toast.LENGTH_LONG).show();

        }

    }else {
        Toast.makeText(getApplicationContext(),"Vous n'avez pas choisi d'image", Toast.LENGTH_LONG).show();

    }
}

}

Thank you in advance.

Leic
  • 57
  • 8

2 Answers2

1

You should never save the image drawable/ bitmap to use in other activities. Instead, you can save the Uri of the image file in some variable in your application class or some static properties holder and then can fetch bitmap from that Uri accross all your activities.

Vishal Arora
  • 2,524
  • 2
  • 11
  • 14
  • Okey, How to do that? please – Leic Jan 24 '19 at 18:43
  • How to create a variable of the image that will be stored in my database? – Leic Jan 24 '19 at 18:45
  • You don't save a variable in database. You save the path denoted by the Uri as text in database. Try saving it in SharedPrefs if you have worked with SharedPrefs. – Vishal Arora Jan 24 '19 at 18:48
  • 1
    `SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext); prefs.edit().putString("imageUri", uri.getPath()).apply(); ` Then you can fetch this uri using - `SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext); String imagePath = prefs.getString("imageUri"); Uri imageUri = Uri.parse(imagePath);` – Vishal Arora Jan 24 '19 at 18:49
  • Yes, but I really need to put it in a database. ;) – Leic Jan 24 '19 at 18:57
  • 1
    For that you will need to create a table in Sqlite Database and insert entries for all the uris. You can reference https://developer.android.com/training/data-storage/sqlite if want to learn how to work with sqlite db. – Vishal Arora Jan 24 '19 at 19:01
  • Or Mysql, it's possible ? – Leic Jan 24 '19 at 19:08
  • Nope, not supported on Android. Sqlite is also a sql based db. The queries are similar. – Vishal Arora Jan 24 '19 at 19:09
  • Sqlite is not Database local ? How save in server ? I think social networks app are backed up with sqlite. – Leic Jan 24 '19 at 19:15
  • Sqlite will be saving it locally on your android device only. – Vishal Arora Jan 24 '19 at 19:17
1

you can use BitmapDrawable to achieve this

//Convert bitmap to drawable.

Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);