Hi new to to Android development. Need help with maintaing the image in imageView when orientation changes. At this moment when i take a pic or upload it, it gets uploaded fine until i switch the orientation. I tried searching from other post by didn't understand well. I suppose it is something to do with save Instance. Could some one please help me with this.
img1.setOnClickListener(new OnClickListener() {
public void onClick(View v){
CharSequence[] names = { "From Gallery", "From Camera" };
new AlertDialog.Builder(context)
.setTitle("Select an option for updating your Profile Picture")
.setItems(names, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int pos) {
// TODO Auto-generated method stub
if (pos == 0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GET_GAL_IMG);
} else {
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(i, GET_CAM_IMG);
}}}
)
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).create().show();
}
});}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case 2://Camera
Log.d("take","pic");
if (resultCode == -1) {
String encodedImageString = null;
Uri selectimage=intent.getData();
Log.d("take","picture");
ImageView img1=(ImageView)findViewById(R.id.img1);
//Changing URI to Bitmap
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectimage));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Reducing Memory
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
byte[] image = baos.toByteArray();
encodedImageString = Base64.encodeToString(image,
Base64.DEFAULT);
} else {
System.out.println("Compression returned false");
Log.d("Compress", "Compression returned false");
}
//setting Imageview as the bitmap so could send it to the canvas
img1.setImageBitmap(bmp);
}
break;
case 1://Selecting from Gallery
Log.d("view","pic");
if (resultCode == -1) {
String encodedImageString = null;
Uri selectimage = intent.getData();
String selectedImagepath = getPath(selectimage);
ImageView img1=(ImageView)findViewById(R.id.img1);
//Changing URI to Bitmap
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectimage));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Reducing Memory
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
byte[] image = baos.toByteArray();
encodedImageString = Base64.encodeToString(image,
Base64.DEFAULT);
} else {
System.out.println("Compression returned false");
Log.d("Compress", "Compression returned false");
}
Log.d("view","picture");
//setting Imageview as the bitmap so could send it to the canvas
img1.setImageBitmap(bmp);
}
break;
}
}
@Waqas solution is a quick fix, that is a wrong decision for different reasons.
In a few words: you are loosing you image because, when the device is rotated the current instance of Activity is lost and a new one is created. A method onSaveInstanceState(Bundle outState) (which you can override) is called each time an activity is going to be destroyed. You can put different kinds of types to the Bundle parameter. When another instance of your Activity is created (e.g. after rotating the screen), you can use the same bundle, in which you put your previous state, to get the previously saved state. The same state can also be retrieved in onRestoreInstanceState.
In your case another problem arises: Bundle is not meant store graphics. To solve that problem you can simply put your graphics data to the memory and then read it from there. In this scenario, you could put the file name and path to the bundle. You might refer to http://developer.android.com/reference/android/app/Activity.html to get some more specific information.