gpt4 book ai didi

java - 恢复用户图像后如何将其保存在 Drawable 中

转载 作者:行者123 更新时间:2023-11-29 04:12:22 26 4
gpt4 key购买 nike

恢复用户图像后,如何将其保存在应用程序的可绘制对象中?用于其他 Activity 。

从用户图库中检索图像并将其放入 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();

}
}

提前谢谢你。

最佳答案

您永远不应该保存图像可绘制对象/位图以在其他 Activity 中使用。相反,您可以将图像文件的 Uri 保存在您的应用程序类中的某个变量或某些静态属性持有者中,然后可以在您的所有 Activity 中从该 Uri 中获取位图。

关于java - 恢复用户图像后如何将其保存在 Drawable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54352821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com