Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android应用程序中,通过摄像头拍照后,屏幕变暗。如何避免这种情况?_Android_Image_Bitmap_Camera_Uri - Fatal编程技术网

在android应用程序中,通过摄像头拍照后,屏幕变暗。如何避免这种情况?

在android应用程序中,通过摄像头拍照后,屏幕变暗。如何避免这种情况?,android,image,bitmap,camera,uri,Android,Image,Bitmap,Camera,Uri,在我的应用程序中,我的目的是允许用户从相机中拍照或从多媒体资料中选择。拍摄或选择照片后,它将显示在ImageView中。所有这些都发生在活动1中,当用户单击编辑按钮时,第二个活动开始,用户可以在其中编辑图片。现在我面临着4个问题,这是我过去几天一直在努力解决的,但我需要一些帮助,因为我是一个Android编程的初学者 从相机拍摄照片后,它将显示在ImageView中,屏幕开始闪烁并变暗 当从相机拍摄时,屏幕上显示的图像尺寸非常小,但当从gallery中选择图像时,屏幕上显示的图像尺寸稍大。这和从

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

  • 从相机拍摄照片后,它将显示在ImageView中,屏幕开始闪烁并变暗
  • 当从相机拍摄时,屏幕上显示的图像尺寸非常小,但当从gallery中选择图像时,屏幕上显示的图像尺寸稍大。这和从相机获取的图像是位图,从gallery获取的图像是Uri有关吗
  • 方向改变时,图像消失。如何在两种模式下保留图像
  • 有时我会忘记例外
  • 这是我的密码:

    活动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);
    }
    
    <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" />
    
    活动2:

    imgView = (ImageView) findViewById(R.id.imgViewEdit_Pic);
    imageBitmap = this.getIntent().getParcelableExtra("post_card");
    
    活动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);
    }
    
    <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" />
    
    
    

    活动2的布局类似


    请尽快有人来帮助我。

    对于该活动,您可以始终打开屏幕,以避免屏幕变暗。谢谢您的回复。这似乎不是解决问题的办法。不过我确实试过了。它使屏幕保持活动状态,但只要我从相机拍摄照片并显示图像,屏幕就会开始闪烁几秒钟,屏幕就会变好