Android Studio - 如何从图库中获取图像然后保存

时间:2022-05-12 06:13:45

I have an app that contains a button. when you press the button, the application will send you to your mobile phone's image gallery. you then select the image and it will appear on an imageview above the button. This all goes without a problem but the only bad thing is that when I close and reopen the app. the image is gone. Does anybody know a way to do this?

我有一个包含按钮的应用程序。当您按下按钮时,应用程序会将您发送到手机的图库。然后选择图像,它将出现在按钮上方的imageview上。这一切都没有问题,但唯一不好的是当我关闭并重新打开应用程序时。图像消失了。有人知道这样做的方法吗?

Here's the code:

这是代码:

Button imageButton = (Button) findViewById(R.id.pictureButton);
        imageButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, 1);
            }
        });

this is the button used to open the gallery.

这是用于打开图库的按钮。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && 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]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.userPicture);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

This is the code tha handles getting the picture and displaying it on the app.

这是代码处理获取图片并在应用程序上显示它。

as for how I save data, I am using sharedpreferences for texts, checkboxes and sliders. but it only doesn't work on images.

至于我如何保存数据,我使用文本,复选框和滑块的共享偏好。但它只适用于图像。

So what I want is the image to be saved so that when I reopen the application, it gets loaded.

所以我想要的是要保存的图像,这样当我重新打开应用程序时,它就会被加载。

1 个解决方案

#1


U could save the picturePath string into a file. A really easy way to do this is by using the Properties object. Then each time when you open the activity, you open the properties file and read out the path that is saved. Then you can set it again with a part of the code that you allready have.

你可以将picturePath字符串保存到文件中。一个非常简单的方法是使用Properties对象。然后,每次打开活动时,都会打开属性文件并读出保存的路径。然后,您可以使用已有的部分代码再次设置它。

#1


U could save the picturePath string into a file. A really easy way to do this is by using the Properties object. Then each time when you open the activity, you open the properties file and read out the path that is saved. Then you can set it again with a part of the code that you allready have.

你可以将picturePath字符串保存到文件中。一个非常简单的方法是使用Properties对象。然后,每次打开活动时,都会打开属性文件并读出保存的路径。然后,您可以使用已有的部分代码再次设置它。