从相机获取图像或从图库上传图像并在ImageView(Android Studio)上显示后,将图像保存为SharedPreferences

时间:2019-04-24 03:40:19

标签: android

我在上面的问题中提到一个问题。来自EditText,RadioButton和Spinner的所有数据都可以保存在SharedPreference中,并显示在另一个活动中。但是我不知道如何从相机获取图像或从图库上传图像并在ImageView中显示后如何保存图像。有什么办法吗?请帮助我。

//用于保存SharedPreferences的代码

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences sharedPref = getSharedPreferences("MyData",MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("title",etTitle.getText().toString());
            editor.putString("year",etYear.getText().toString());
            editor.putString("month",etMonth.getText().toString());

            // get selected radio button from radioGroup
            int selectedId = rgSuggestWill.getCheckedRadioButtonId();

            // find the radiobutton by returned id
            radioButton = findViewById(selectedId);
            editor.putString("suggestionwill",radioButton.getText().toString());
            if (spReviewer.getSelectedItem().toString().equals("Please choose")){

                AlertDialog alertDialog = new AlertDialog.Builder(NewSuggestion.this).create();
                alertDialog.setTitle("Alert");
                alertDialog.setMessage("Please choose your reviewer");
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();
            }else{
                editor.putString("reviewer",spReviewer.getSelectedItem().toString());
                Intent intent = new Intent(NewSuggestion.this,NewSuggestion2.class);
                startActivity(intent);
            }
            editor.commit();
        }
    });

//用于从SharedPreferences撤回的代码

SharedPreferences sharedPreferences = getSharedPreferences("MyData", MODE_PRIVATE);
    String title = sharedPreferences.getString("title",DEFAULT);
    String year = sharedPreferences.getString("year",DEFAULT);
    String month =  sharedPreferences.getString("month",DEFAULT);
    String present =  sharedPreferences.getString("present",DEFAULT);
    String details = sharedPreferences.getString("details",DEFAULT);
    String benefit =  sharedPreferences.getString("benefit",DEFAULT);
    String suggestionwill =  sharedPreferences.getString("suggestionwill",DEFAULT);
    String reviewer =  sharedPreferences.getString("reviewer",DEFAULT);

4 个答案:

答案 0 :(得分:1)

不要将图像存储在SharedPreferences中,您应该将图像保存到sd卡中,然后将图像从sd卡保存到SharedPreferences中->

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.edit().putString("YourImagePathTag","YourImagePath").commit;

然后您可以从此路径获取图像。另外,您可以将路径保存到数据库中,比这更安全。

答案 1 :(得分:0)

不要使用首选项保存图像,而是使用SQLite通过使用“ blob”进行保存和检索

///将位图转换为base64

public static String encodeBitmapTobase64(Bitmap image) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, os);
    byte[] byteArray = baos.toByteArray();
    String encodedImageString = 
    Base64.getEncoder().encodeToString(byteArray);
    return encodedImageString ;
}

//按首选项保存图像

SharedPreferences.Editor prefEditor= myPrefrence.edit();
prefEditor.putString("key", encodeBitmapTobase64(yourbitmap));
prefEditor.commit();

//从pref获取编码的字符串,并将base64字符串转换为位图

public static Bitmap base64ToBitmap(String encodedString) {
    byte[] decodedString = Base64.decode(encodedString, Base64.DEFAULT);
    Bitmap bitmap= BitmapFactory.decodeByteArray(decodedString , 0, 
    decodedString.length);
    return bitmap;
}

答案 2 :(得分:0)

(按字符串)将图像保存在sharedpreference中不是一个好方法。 将图像制作为字节数组或uri(保存在EXTERNAL_STORAGE中之后)并使用intent.putextra

答案 3 :(得分:0)

例如,通过onActivityResult()方法从图库中获取选定的图像时,可以像这样从Uri保存路径字符串:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result code is RESULT_OK only if the user selects an Image
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case GALLERY_REQUEST_CODE:
                //data.getData returns the content URI for the selected Image
                Uri selectedImage = data.getData();

                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
                sharedPreferences.edit().putString("KEY_IMAGE", selectedImage.getPath()).apply();
                break;
        }
    }
}