如何在片段Android中捕获图像

时间:2014-09-03 08:36:35

标签: android camera fragment

我正在处理捕获图像,然后在我的片段中的ImageView上显示它。我通过网络搜索了这个,但我仍然无法做到。 以下是我正在做的事情。

btnCaptureImg.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(cameraIntent, 1888); 
    }
});

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e("LOG", ""+requestCode);
    if (requestCode == 1888 && resultCode == Activity.RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }

    super.onActivityResult(requestCode, resultCode, data);
}  

Logcat只说1888但imageView没有加载图像。

我也试过这个

btnCaptureImg.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        File folder = new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/");
        folder.mkdirs();

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        resultingFile = new File(folder.toString() + "/image.jpg");
        Uri uriSavedImage=Uri.fromFile(resultingFile);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

        startActivityForResult(cameraIntent, 1888);
    }
});

但它抛出异常:

E/AndroidRuntime(4824): java.lang.RuntimeException: Unable to resume activity {com.myapp.android/com.myapp.android.MyHomeActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=264032, result=-1, data=null} to activity {com.myapp.android/com.myapp.android.MyHomeActivity}: java.lang.NullPointerException
E/AndroidRuntime(4824):     at com.myapp.android.MyFragment.onActivityResult(MyFragment.java:300)

3 个答案:

答案 0 :(得分:0)

检查是否正在调用onActicityResult。如果没有检查: onActivityResult is not being called in Fragment

答案 1 :(得分:0)

尝试放

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, your_image_uri);

之前

startActivityForResult(cameraIntent, 1888);

我很抱歉,但我似乎无法理解为什么会给出这个例外。尝试这种方法,它适合我。

全局和onActivityResult

内定义文件
    if (requestCode == 1888 && resultCode == Activity.RESULT_OK) { 
        Bitmap  photo = Media.getBitmap(getActivity().getContentResolver(), Uri.fromFile(resultingFile)); 
        //Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }

答案 2 :(得分:0)

首先,在片段中启动捕获图像的意图:

private void startIntentImageCapture() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile("imageName.jpg", ".jpg", storageDir);
        path = Uri.fromFile(image);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, path);
        getActivity().startActivityForResult(cameraIntent, CAPTURE_IMAGE_REQUEST_CODE);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

path变量,必须是公共静态Uri实例。请求代码的另一个公共静态Integerpublic static final Integer CAPTURE_IMAGE_REQUEST_CODE = 100;

然后在Activity中,阅读此路径并解码图像:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == YourFragment.CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Bitmap imageBitmap = decodeFile(YourFragment.path);
                //do something with your photo
            }
        }
    }

我确信这不是最好,更清洁的方法,但它对我有用。希望它有所帮助!

修改

我忘记了解码图片的代码。

 public Bitmap decodeFile(Uri uriFile) throws OutOfMemoryError {
        String filePath = uriFile.getPath();
        BitmapFactory.Options bmOptions;
        Bitmap imageBitmap;
        try {
            imageBitmap = BitmapFactory.decodeFile(filePath);
        } catch (OutOfMemoryError e) {
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 4;
            bmOptions.inPurgeable = true;
            imageBitmap = BitmapFactory.decodeFile(filePath, bmOptions);
        }
        return imageBitmap;
    }

bmOptions确保不会发生OutOfMemoryError。