Android的 - 的ImageView我怎样才能获得摄像机图像?其中,结果code为0,并要求code为2? [英] Android - ImageView how can i get the camera picture? where resultCode is 0 and requestCode is 2?

查看:141
本文介绍了Android的 - 的ImageView我怎样才能获得摄像机图像?其中,结果code为0,并要求code为2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取结果code = 0,并要求code = 2,不能获得与picView画面。

 进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;进口android.app.Activity;
进口android.content.ActivityNotFoundException;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.net.Uri;
进口android.os.Bundle;
进口android.provider.MediaStore;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.Toast;公共类MainActivity扩展活动实现OnClickListener {
    最终诠释CAMERA_CAPTURE = 1;
    最终诠释PIC_CROP = 2;
    私人乌里picUri;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        按钮captureBtn =(按钮)findViewById(R.id.capture_btn);
        captureBtn.setOnClickListener(本);
    }    / **
     *点击的方法来处理用户pressing按钮启动相机
     * /
    公共无效的onClick(视图v){
        如果(v.getId()== R.id.capture_btn){
            尝试{
                意图captureIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
                startActivityForResult(captureIntent,CAMERA_CAPTURE);
            }
            赶上(ActivityNotFoundException anfe){
                字符串的errorMessage =哎呦 - 你的设备不支持拍摄照片!
                吐司面包= Toast.makeText(这一点,的errorMessage,Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }    / **
     *处理用户来自捕获返回和裁剪图像
     * /
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK){
            如果(要求code == CAMERA_CAPTURE){
                picUri = data.getData();
                performCrop();
            }
            否则,如果(要求code == PIC_CROP){
                捆绑额外= data.getExtras();
                位图thePic = extras.getParcelable(数据);
                ImageView的picView =(ImageView的)findViewById(R.id.picture);
                picView.setImageBitmap(thePic);
            }
        }其他{
            字符串的errorMessage =哎呦 - 结果code的回报:+结果code +的要求code:+请求code;
            吐司面包= Toast.makeText(这一点,的errorMessage,Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    / **
     * Helper方法来进行操作作物
     * /
    私人无效performCrop(){
        尝试{
            意图cropIntent =新意图(com.android.camera.action.CROP);
            cropIntent.setDataAndType(picUri,图像/ *);
            cropIntent.putExtra(裁剪,真);
            cropIntent.putExtra(aspectX,1);
            cropIntent.putExtra(aspectY,1);
            cropIntent.putExtra(outputX,256);
            cropIntent.putExtra(outputY,256);
            cropIntent.putExtra(回归数据,真正的);
            startActivityForResult(cropIntent,PIC_CROP);
        }
        赶上(ActivityNotFoundException anfe){
            字符串的errorMessage =哎呦 - 你的设备不支持作物的行动!
            吐司面包= Toast.makeText(这一点,的errorMessage,Toast.LENGTH_SHORT);
            toast.show();
        }
    }}

修改(添加为以下,其中data.getData()始终为null)

 最终诠释CAPTURE_IMAGE_ACTIVITY_REQUEST_ code = 100;
最终诠释CAPTURE_VIDEO_ACTIVITY_REQUEST_ code = 200;公共无效的onClick(视图v){
        如果(v.getId()== R.id.capture_btn){
            尝试{
                意图captureIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
                // startActivityForResult(captureIntent,CAMERA_CAPTURE);
                startActivityForResult(captureIntent,CAPTURE_IMAGE_ACTIVITY_REQUEST_ code);
            }
            赶上(ActivityNotFoundException anfe){
                字符串的errorMessage =哎呦 - 你的设备不支持拍摄照片!
                吐司面包= Toast.makeText(这一点,的errorMessage,Toast.LENGTH_SHORT);
                toast.show();
            }
        }
}    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == CAPTURE_IMAGE_ACTIVITY_REQUEST_ code){
            如果(结果code == RESULT_OK){
                //图像捕获,并在指定的意图保存到了fileURI
                Toast.makeText(这一点,图像保存到:\\ n+
                         data.getData(),Toast.LENGTH_LONG).show();
            }否则如果(结果code == RESULT_CANCELED){
                //用户取消了图像采集
            }其他{
                //图像捕捉失​​败,提醒用户
            }
        }        如果(要求code == CAPTURE_VIDEO_ACTIVITY_REQUEST_ code){
            如果(结果code == RESULT_OK){
                //视频捕获,并在指定的意图保存到了fileURI
                Toast.makeText(这一点,视频保存到:\\ n+
                         data.getData(),Toast.LENGTH_LONG).show();
            }否则如果(结果code == RESULT_CANCELED){
                //用户取消了视频拍摄
            }其他{
                //视频捕获失败,建议用户
            }
        }
    }

编辑2:

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    //super.onActivityResult(request$c$c,结果code,数据);
    如果(结果code == Activity.RESULT_OK){
        如果(要求code == CAMERA_CAPTURE){
            picUri = data.getData();
            performCrop();
        }
        否则,如果(要求code == PIC_CROP){
            捆绑额外= data.getExtras();
            位图thePic = extras.getParcelable(数据);
            ImageView的picView =(ImageView的)findViewById(R.id.picture);
            picView.setImageBitmap(thePic);
        }
    }其他{
        字符串的errorMessage =哎呦 - 结果code的回报:+结果code +的要求code:+请求code;
        吐司面包= Toast.makeText(这一点,的errorMessage,Toast.LENGTH_SHORT);
        toast.show();
        捆绑额外= data.getExtras();
        位图thePic = extras.getParcelable(数据);
        ImageView的picView =(ImageView的)findViewById(R.id.picture);
        picView.setImageBitmap(thePic);
    }
}


解决方案

如果结果code是0,那么就意味着你调用的活动被取消。他们要么必须有pressed后退按钮,职业退休回来了此次活动。改变你的实现是错误的。

Getting resultCode= 0 and requestCode = 2, cant get the picView with picture.

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity  implements OnClickListener {
    final int CAMERA_CAPTURE = 1;
    final int PIC_CROP = 2;
    private Uri picUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button captureBtn = (Button)findViewById(R.id.capture_btn);
        captureBtn.setOnClickListener(this);        
    }

    /**
     * Click method to handle user pressing button to launch camera
     */
    public void onClick(View v) {
        if (v.getId() == R.id.capture_btn) {     
            try {
                Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(captureIntent, CAMERA_CAPTURE);
            }
            catch(ActivityNotFoundException anfe){
                String errorMessage = "Whoops - your device doesn't support capturing images!";
                Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }

    /**
     * Handle user returning from both capturing and cropping the image
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if(requestCode == CAMERA_CAPTURE){
                picUri = data.getData();
                performCrop();
            }
            else if(requestCode == PIC_CROP){
                Bundle extras = data.getExtras();
                Bitmap thePic = extras.getParcelable("data");
                ImageView picView = (ImageView)findViewById(R.id.picture);
                picView.setImageBitmap(thePic);
            }
        } else {
            String errorMessage = "Whoops - resultCode return: " + resultCode  +" requestCode: " + requestCode;
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show(); 
        }
    }    


    /**
     * Helper method to carry out crop operation
     */
    private void performCrop(){
        try {
            Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
            cropIntent.setDataAndType(picUri, "image/*");
            cropIntent.putExtra("crop", "true");
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);
            cropIntent.putExtra("return-data", true);
            startActivityForResult(cropIntent, PIC_CROP);  
        }
        catch(ActivityNotFoundException anfe){
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }    

}

EDIT: (added as following where data.getData() is null always)

final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

public void onClick(View v) {
        if (v.getId() == R.id.capture_btn) {     
            try {
                Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //startActivityForResult(captureIntent, CAMERA_CAPTURE);
                startActivityForResult(captureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            }
            catch(ActivityNotFoundException anfe){
                String errorMessage = "Whoops - your device doesn't support capturing images!";
                Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
                toast.show();
            }
        }
}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // Image captured and saved to fileUri specified in the Intent
                Toast.makeText(this, "Image saved to:\n" +
                         data.getData(), Toast.LENGTH_LONG).show();
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
            } else {
                // Image capture failed, advise user
            }
        }

        if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // Video captured and saved to fileUri specified in the Intent
                Toast.makeText(this, "Video saved to:\n" +
                         data.getData(), Toast.LENGTH_LONG).show();
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the video capture
            } else {
                // Video capture failed, advise user
            }
        }
    } 

EDIT 2:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if(requestCode == CAMERA_CAPTURE){
            picUri = data.getData();
            performCrop();
        }
        else if(requestCode == PIC_CROP){
            Bundle extras = data.getExtras();
            Bitmap thePic = extras.getParcelable("data");
            ImageView picView = (ImageView)findViewById(R.id.picture);
            picView.setImageBitmap(thePic);
        }
    } else {
        String errorMessage = "Whoops - resultCode return: " + resultCode  +" requestCode: " + requestCode;
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show(); 
        Bundle extras = data.getExtras();
        Bitmap thePic = extras.getParcelable("data");
        ImageView picView = (ImageView)findViewById(R.id.picture);
        picView.setImageBitmap(thePic);         
    }
} 

解决方案

If resultCode is 0 then it means your invoked activity was cancelled. Either they must have pressed back button, orso to come back to this activity. Changes are your implementation is wrong.

这篇关于Android的 - 的ImageView我怎样才能获得摄像机图像?其中,结果code为0,并要求code为2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆