chen_gong1992
MrCG
2016-07-20 11:42

android调用系统相机拍照报错,onActivityResult中data为null

5
  • android

这是书上的一个练习,对着敲的,运行时拍照后点完成就出错,什么原因啊?怎么解决
图片说明

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
public static final int TAKE_PHOTO=1;
public static final int CROP_PHOTO=2;
private Button takePhoto;
private ImageView picture;
private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 //获取控件实例
    takePhoto=(Button) findViewById(R.id.take_photo);
    picture=(ImageView) findViewById(R.id.picture);
    takePhoto.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //创建File对象,拥有储存拍的照片
            File outputImage=new File(Environment.getExternalStorageDirectory(),"outputImag.jpg");
            //getExternalStorageDirectory()获取手机sd卡的根目录
            try{
                if(outputImage.exists()){
                    outputImage.delete();
                }
                outputImage.createNewFile();
            }catch(IOException e){
                e.printStackTrace();
            }
                //将File对象转换成Uri对象
                imageUri=Uri.fromFile(outputImage);
                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //指定保存路径
                startActivityForResult(intent,TAKE_PHOTO);//启动相机程序   拍完照后返回到onAcitvityResult()
        }
        });
        }  
     protected void onActivityResult(int requestCode, int resultCode, Intent data ){
         Log.d("MainActivity", "ok");
         switch(requestCode){
         case TAKE_PHOTO:        
         if (resultCode==RESULT_OK){
                 //执行裁剪照片的逻辑
                 Intent intent=new Intent("come.android.camera.action.CROP");
                 intent.setDataAndType(imageUri, "image/*");
                 intent.putExtra("crop", true);
                 intent.putExtra("scale", true);
                 intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);//输出文件
                 startActivityForResult(intent, CROP_PHOTO); // 启动裁剪程序  
             }
         break;
         case CROP_PHOTO:
             if(resultCode==RESULT_OK){  
                try {
                    // 用BitmapFactory的decodeStream()方法将output_image.jpg这张照片解析成Bitmap对象
                    Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));  
                    picture.setImageBitmap(bitmap);//显示裁剪的照片
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
             }
               break;
             default:
               break;
         }
     }
     }

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

8条回答