In my app, my intention is to allow a user to either take a photo from the camera or select from the gallery. Once the photo is taken or chosen it is displayed in an ImageView. All this happens in Activity 1 and then when the user clicks the edit button the 2nd Activity starts where user can edit the picture. Now I am facing 4 problems which I have been trying to solve from past few days but I need some assistance as I am a beginner in Android Programming.
- After taking a photo from camera, it is displayed in an ImageView and the screen starts to flicker and becomes dim.
- Image size shown on screen is very small when taken from camera but slightly bigger when image is selected from gallery.Does it have anything to do with the image as image got from camera is a Bitmap and from gallery is a Uri?
- Image disappears when orientation is changed. How to retain the image in both modes?
- Sometimes I get OutOfMemoryException
Here is my code:
Activity 1:
public class HowItWorksActivity extends Activity {
ImageButton btn_Account,btn_Photo,btn_Edit,btn_Flip,btn_Post;
RelativeLayout rl;
private static final int CAMERA_PIC_REQUEST = 2500;
private static final int SELECT_PICTURE = 1;
Bitmap bmap_image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_how_it_works);
addListenerOnButton();
}
private void addListenerOnButton() {
btn_Photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PopupMenu photo_menu = new PopupMenu(HowItWorksActivity.this, v);
photo_menu.getMenuInflater().inflate(R.menu.photo_menu, photo_menu.getMenu());
photo_menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.take_a_picture:
takepicture();
break;
case R.id.upload_photo:
getPicture();
break;
}
return true;
}
private void getPicture() {
// TODO Auto-generated method stub
Intent getPicIntent = new Intent();
getPicIntent.setType("image/*");
getPicIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(getPicIntent, "Select Picture"), SELECT_PICTURE);
}
private void takepicture() {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
photo_menu.show();
}
});
btn_Edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent editIntent = new Intent(HowItWorksActivity.this,EditPhotoActivity.class);
if (bmap_image !=null)
editIntent.putExtra("post_card", bmap_image);
startActivity(editIntent);
}
});
@SuppressWarnings("deprecation")
protected void onActivityResult(int requestCode,int resultCode,Intent data){
Bitmap image = null;
ImageView imgview = (ImageView) findViewById(R.id.ImgViewForPic);
rl = (RelativeLayout) findViewById(R.id.R_layoutPic);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.bg);
rl.setBackgroundDrawable(drawable);
if(requestCode == CAMERA_PIC_REQUEST){
image = (Bitmap) data.getExtras().get("data");
bmap_image = image;
imgview.setImageBitmap(image);
}
if (resultCode == RESULT_OK){
if(requestCode == SELECT_PICTURE && data!=null && data.getData()!=null){
Uri selectedImgUri = data.getData();
Cursor cursor = getContentResolver().query(selectedImgUri, new String[] {MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();
cursor.close();
try {
bmap_image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImgUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imgview.setImageURI(selectedImgUri);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
if(bmap_image!=null)
outState.putParcelable("post_card", bmap_image);
super.onSaveInstanceState(outState);
}
Activity 2:
imgView = (ImageView) findViewById(R.id.imgViewEdit_Pic);
imageBitmap = this.getIntent().getParcelableExtra("post_card");
Layout file of Activity 1:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/R_layoutPic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/instruction_landscape"
tools:context=".HowItWorksActivity" >
<RelativeLayout
android:id="@+id/RlayoutButtons"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:background="@drawable/bottom_nav" >
<ImageButton
android:id="@+id/btn_Edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/btn_Photo"
android:contentDescription="@string/content"
android:paddingTop="10dp"
android:src="@drawable/icon_edit" />
<ImageButton
android:id="@+id/btn_Photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Account"
android:layout_toRightOf="@+id/btn_Account"
android:contentDescription="@string/content"
android:paddingTop="10dp"
android:src="@drawable/icon_pickphoto" />
</RelativeLayout>
<ImageView
android:id="@+id/ImgViewForPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/RlayoutButtons"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_marginBottom="188dp"
android:contentDescription="@string/content" />
Layout of Activity 2 is similar.
Please somebody help me asap.