gpt4 book ai didi

Android 从图库中选择图片

转载 作者:行者123 更新时间:2023-11-29 01:13:50 25 4
gpt4 key购买 nike

我从图库中选择了一张图片,现在我想要的是当用户重新打开应用程序时,图片就在 ImageView 中,请给我一些建议,这是我的代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();

imgView = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.profileimg);
SharedPreferences.Editor editor = getSharedPreferences(AppConstants.VERIFICATION, MODE_PRIVATE).edit();
editor.putString(AppConstants.PROFILEIMAGE, imgDecodableString);
editor.commit();
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}

最佳答案

试试这段代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();

imgView = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.profileimg);
Bitmap bmap = imgView.getDrawingCache();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.PNG, 90, bytes);
byte[]imagebytes=bytes.toByteArray();
String encodedImage = Base64.encodeToString(imagebytes, Base64.DEFAULT);
SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(ProfilePage.this);
SharedPreferences.Editor edit=shre.edit();
edit.putString("image_data",encodedImage);
edit.commit();
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}

添加下面的代码以获得共享偏好

 SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
String previouslyEncodedImage = shre.getString("image_data", "");
if( !previouslyEncodedImage.equalsIgnoreCase("") ){
byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
imgView.setImageBitmap(bitmap);
}

关于Android 从图库中选择图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41177962/

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