tags:

views:

490

answers:

0

I have an Image view that displays a image selected by the user from the device. When first opening the Activity, the image displays fine, but if they rotate the device, I save the selected Image's Uri to the savedInstanceState in onSaveInstanceState() and try to pull it back in the onCreate(). In onCreate the Uri is parced back out fine, but when I call ImageView.setImageURI i get in error in BitmapFactory.class. Please, what am I doing wrong?

    @Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 try {
  setContentView(R.layout.upload);
 } catch (Exception e) {
  Log.e("ERROR", "ERROR IN CODE:"+e.toString());
 }

 if (savedInstanceState != null && savedInstanceState.containsKey("SelectedImage")) {
  String uri = savedInstanceState.getString("SelectedImage");
  uriSelectedImage=Uri.parse(uri);
 }

 if (uriSelectedImage == null) {
  // Look in the Device
  startActivityForResult(new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),
    1);
  // Look in the SD Card
  // startActivityForResult(new Intent(Intent.ACTION_PICK,
  // android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), 1);
 } else {
  ImageView ivImage = (ImageView)findViewById(R.id.ivImage);
  ivImage.setImageURI(uriSelectedImage);
 }

 Button btnSend = (Button) findViewById(R.id.btnSend);
 btnSend.setOnClickListener(btnSend_Click);
}

Saving the state when the device is rotated:

    @Override
public void onSaveInstanceState(Bundle savedInstanceState) {
 super.onSaveInstanceState(savedInstanceState);
 if (uriSelectedImage != null) {
  String uri = uriSelectedImage.toString();
  savedInstanceState.putString("SelectedImage", uri);
 }
}

I have the stack trace of the BitmapFactory, if needed let me know and I'll post it. I just didnt want to initially because its a lot of text.