-2

I get an image from the camera or gallery and set it to my imageview. Unfortunately when the screen gets rotated the imageview shows nothing. How can I keep the image visible ?

This is the code where I get the image :

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

    switch (requestCode){
        case REQUEST_IMAGE_CAPTURE:

    // if taking
    if (resultCode == RESULT_OK && null != data  && TAKE_OR_PICK == 1) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");

        int width = imageBitmap.getWidth();
        int height = imageBitmap.getHeight();
        if(height>width) {
            int crop = (height - width) / 2;
            Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, width, width);
            image.setImageBitmap(cropImg);
        }
        else if (width>height){int crop = (width - height) / 2;
            Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, height, height);
            image.setImageBitmap(cropImg);}
        else {image.setImageBitmap(imageBitmap);}

    }
        break;

        case RESULT_LOAD_IMAGE:
        // if choosing
    if (resultCode == RESULT_OK && null != data  && TAKE_OR_PICK == 2) {
        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();
        Bitmap imageBitmap = BitmapFactory.decodeFile(picturePath);
        int width = imageBitmap.getWidth();
        int height = imageBitmap.getHeight();
        if(height>width) {
            int crop = (height - width) / 2;
            Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, width, width);
            image.setImageBitmap(cropImg);
        }
        else if (width>height){int crop = (width - height) / 2;
            Bitmap cropImg = Bitmap.createBitmap(imageBitmap, crop, 0, height, height);
            image.setImageBitmap(cropImg);
        }
        else {image.setImageBitmap(imageBitmap);}

    }

I've tried to use onSaveInstanceState() but can't get it to work for a bitmap. Is there another way to achieve this ?

Hans1984
  • 796
  • 11
  • 24
behrooz
  • 617
  • 9
  • 30

3 Answers3

1

There are generally three ways to do this:

  1. As some of the answers suggested, you could distinguish the cases of your activity being created for the first time and being restored
    from savedInstanceState. This is done by overriding
    onSaveInstanceState and checking the parameter of onCreate.

    2.You could lock the activity in one orientation by adding android:screenOrientation="portrait" (or "landscape") to in your manifest.

    3.You could tell the system that you meant to handle screen changes for yourself by specifying
    android:configChanges="orientation" in the tag. This way the activity will not be recreated, but will receive a callback
    instead (which you can ignore as it's not useful for you)

Salauddin Gazi
  • 1,497
  • 1
  • 13
  • 18
  • Thanks for your fast reply , can you explain 1 more ? I can't use 2 because i want it to rotate . have no comment at 3 . – behrooz Jul 29 '15 at 09:35
  • @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted. ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); savedInstanceState.putExtra("image",byteArray); // etc. } and add – Salauddin Gazi Jul 29 '15 at 10:04
  • @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Restore UI state from the savedInstanceState. // This bundle has also been passed to onCreate. byte[] byteArray = savedInstanceState.getByteArrayExtra("image"); Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);image.setImageBitmap(bmp); } – Salauddin Gazi Jul 29 '15 at 10:05
0

What happening in your case is, if you rotate your device the activity is recreated.Try adding the below inside activity tag of your AndroidManifest.xml file. Hope this helps.

android:configChanges="orientation | screenSize"
Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
0

as Gazi said if the user is not allowed to rotate the screen that "close" the option via the Manifest.

if it's not relevant you shall have a function that

fillAllData()

that is called within "onResume" and inside the function if you have the bitmap add it into the image (you can keep it with onSaveInstance)

    private ImageView mMyImageView;
    private String tempFileName;

    onCreate(){
      setContentView(R.layout.myLayout);
      mMyImageView = (ImageView)findViewById(R.id.myImageView);
    }

    onActivityResult(){
       //do all your code plus this:
      tempFileName = random string;
      save bitmap after process in tempFileName
      update in application some flag that a temp file image is available;
    }

    onResume(){
      if temp file name is available load bitmap into your image view.
    }
danysz
  • 580
  • 3
  • 11