【发布时间】:2019-01-24 18:00:06
【问题描述】:
一旦你恢复了用户的图像,如何将它保存在应用程序中的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();
}
}
}
提前谢谢你。
【问题讨论】:
-
什么意思??(请多解释),我想你不能这样,你可以把它保存在本地或服务器。
-
是的,如何创建将存储在我的数据库中的图像变量?
标签: java android android-studio save