Android 从图库中选择图片

标签 android android-imageview

我从图库中选择了一张图片,现在我想要的是当用户重新打开应用程序时,图片就在 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/

相关文章:

Android WorkManager : how to handle ongoing work, 喜欢蓝牙扫描

android - 如何使用 Android 应用程序管理 session

Android如何设置一个 View 的透明图片资源?或删除图像?

android - 屏幕 ImageView 中的替代图像

android - 按下时更改 Android ListView 条目中的图标

android - Android 的 Sqlite 加密

android - 由于 "multiple process",即时运行不起作用

android - 在 EditText 上实现自动填充

android - 如何在 `PhotoView` 上显示文字?

android - 使用 "match_parent"将图像对齐到顶部