java一旦恢复了用户的图像,如何将其保存在Drawable中
恢复用户的图像后,如何将其保存在应用程序中的drawable中? 用于其他活动
从用户的图库中检索图像并将其放入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();
}
}
}
先谢谢你
# 1 楼答案
永远不要保存图像可绘制/位图以用于其他活动。相反,您可以将图像文件的Uri保存在应用程序类中的某个变量或某个静态属性持有者中,然后可以从该Uri中获取位图,并访问所有活动
# 2 楼答案
您可以使用BitmapDrawable来实现这一点
//将位图转换为可绘制
Drawable Drawable=新的BitmapDrawable(context.getResources(),位图)
# 3 楼答案
您可以将位图转换为字节数组,并将其保存在域数据库中强>
像这样:
注意:Realm支持以下字段类型:boolean、byte、short、ìnt、long、float、double、String、Date和byte[]
您也可以在android中使用this进行领域配置强>
您可以通过以下方式将图像视图转换为位图: