恢复用户的图像后,如何将其保存在Drawable中 [英] Once you have recovered an image of the user how to save it in Drawable
本文介绍了恢复用户的图像后,如何将其保存在Drawable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
恢复用户的图像后,如何将其保存在应用程序的drawable中?用于其他活动.
Once you have recovered an image of the user, how to save it in drawable in the app ? For use it in other activities.
从用户的库中检索图像并将其放入ImageView中:
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();
}
}
}
谢谢.
推荐答案
您可以将位图转换为字节数组,并将其保存在领域数据库中.
像这样:
Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
注意:Realm支持以下字段类型:布尔,字节,短,ìnt,长,浮点,双精度,字符串,日期和字节[]
Notice:Realm supports the following field types: boolean, byte, short, ìnt, long, float, double, String, Date and byte[]
您也可以将此用于android中的领域配置.
You can use this for realm configuration in android too.
您可以通过以下方式将图像视图转换为位图:
And you can convert imageview to bitmap by :
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
这篇关于恢复用户的图像后,如何将其保存在Drawable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文