0

在我的应用程序中,我的意图是允许用户从相机拍照或从图库中选择。拍摄或选择照片后,它会显示在 ImageView 中。所有这些都发生在活动 1 中,然后当用户单击编辑按钮时,第二个活动开始,用户可以在其中编辑图片。现在我面临着过去几天一直在尝试解决的 4 个问题,但我需要一些帮助,因为我是 Android 编程的初学者。

  1. 从相机拍摄照片后,它显示在 ImageView 中,屏幕开始闪烁并变暗。
  2. 从相机拍摄时屏幕上显示的图像尺寸非常小,但从图库中选择图像时略大。它与图像有什么关系,因为从相机获取的图像是位图,而从图库中获取的图像是 Uri?
  3. 改变方向时图像消失。如何在两种模式下保留图像?
  4. 有时我得到 OutOfMemoryException

这是我的代码:

活动一:

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);
}

活动二:

imgView = (ImageView) findViewById(R.id.imgViewEdit_Pic);
imageBitmap = this.getIntent().getParcelableExtra("post_card");

活动一的布局文件:

<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" />

Activity 2 的布局类似。

请有人尽快帮助我。

4

0 回答 0