应用关闭并重新启动后恢复的ImageView

时间:2016-10-04 19:28:35

标签: imageview sharedpreferences restore image-gallery

关闭并重新启动应用后如何恢复从图库中挑选的ImageView。我正在使用SharedPreferences。要在关闭应用程序后保存URI的状态,但是它无法正常工作,图像不会再次设置,任何帮助都会受到欢迎。

public class test extends AppCompatActivity {

private static int RESULT_LOAD_IMAGE = 1;
ImageButton buttonLoadImage;

private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;

public String getImagePathFetch;
ImageView imgView;

public String keyImage = "myImage";
public SharedPreferences sharedPrefEnter, sharedPrefGet;
Intent galleryIntent;
Uri selectedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity_layout);

    imgView = (ImageView) findViewById(R.id.imageView1);

    sharedPrefGet = PreferenceManager.getDefaultSharedPreferences(this);
    getImagePathFetch = sharedPrefGet.getString(keyImage, "");

    if (!getImagePathFetch.equals("")) {
        // tToast("OnCreate Path=" + getImagePathFetch);

        Uri uri;
        uri = Uri.parse(getImagePathFetch);
        imgView.setImageURI(uri);
    }

    buttonLoadImage = (ImageButton) findViewById(R.id.buttonLoadPicture);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            AddImage(view);

        }
    });

}

public void AddImage(View view) {

    galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

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

    // When an Image is picked
    if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
        // Get the Image from data

        selectedImage = data.getData();

        sharedPrefEnter = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPrefEnter.edit();
        editor.putString(keyImage, selectedImage.toString());
        editor.commit();

        Uri uri = Uri.parse(selectedImage.toString());
        imgView.setImageURI(uri);

    }

}

}

1 个答案:

答案 0 :(得分:0)

    // get the path of the image
    sharedPrefGet = PreferenceManager.getDefaultSharedPreferences(this);
    getImagePathFetch = sharedPrefGet.getString(keyImage, "");

    if (!getImagePathFetch.equals("")) {
        // tToast("OnCreate Path = " + getImagePathFetch); //TESTING

        // parse path from toString to uri
        uriImage = Uri.parse(getImagePathFetch);

        // explicitly check for the permission at runtime since API 23
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        // Permission not granted
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                    WRITE_EXTERNAL_STORAGE);
        } else // Permission granted
        {

            InputStream imageStream = null;
            try {
                imageStream = getContentResolver().openInputStream(uriImage);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
            Bitmap imageResized = getResizedBitmap(yourSelectedImage, 480, 480); // resized
                                                                                    // Image
            imgView.setImageBitmap(imageResized);

        } // end if

    } 

//检查权限结果。     @覆盖     public void onRequestPermissionsResult(int requestCode,@ NonNull String []权限,             @NonNull int [] grantResults){         switch(requestCode){

    case WRITE_EXTERNAL_STORAGE:
        if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
            InputStream imageStream = null;
            try {
                imageStream = getContentResolver().openInputStream(uriImage);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
            Bitmap imageResized = getResizedBitmap(yourSelectedImage, 480, 480); // resized
                                                                                    // Image
            imgView.setImageBitmap(imageResized);
        }
        break;

    default:
        break;
    }
}