Once you have recovered an image of the user how to save it in Drawable

99封情书 提交于 2019-12-13 18:53:11

问题


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.


回答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.




回答2:


you can use BitmapDrawable to achieve this

//Convert bitmap to drawable.

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




回答3:


You can convert your bitmap to byte array and save it in realm database.

like this :

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Notice:Realm supports the following field types: boolean, byte, short, ìnt, long, float, double, String, Date and byte[]

You can use this for realm configuration in android too.

And you can convert imageview to bitmap by :

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();


来源:https://stackoverflow.com/questions/54352821/once-you-have-recovered-an-image-of-the-user-how-to-save-it-in-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!