现在的位置: 首页 > 综合 > 正文

andorid在 Fragment中调用摄像机

2019年09月11日 ⁄ 综合 ⁄ 共 3370字 ⁄ 字号 评论关闭
文章目录

我现在一个activity中有三个multiple fragments ,我现在采集我第三个 fragments信息

在这个fragments 上我想启动相机或者图库,看下面代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public Intent getImageIntent() {
 
    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = context.getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName,
                res.activityInfo.name));
        intent.setPackage(packageName);
        cameraIntents.add(intent);
    }
 
    // Filesystem.
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
 
    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
 
    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[] {}));
 
    // Calling activity should exeecute:
    // startActivityForResult(chooserIntent, 1);
    return chooserIntent;
}
 
After that the onActivityResult executes:
 
private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mProductBitmap = (Bitmap) extras.get("data");
    imgProduct.setImageBitmap(mProductBitmap);
}

其中 mProductBitmap  是一个Bitmap类型的全局变量,imgProduct  是一个已经初始化的 ImageView ,

我现在有如下问题:
   1、相机选项强制关闭了app.并在fragment  中报错了nullpointException
   2、图册选项不报错,但是不显示任何图片
   3、有的时候  在执行完onActivityResult 之后Context 为null

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
 
    if (resultCode == Activity.RESULT_OK) {
        handleSmallCameraPhoto(intent);
    else {
        if (requestCode == 1) {
            InputStream stream = null;
            if (intent == null) {
                System.out.println("DATA IS NULL..");
            else {
                try {
                    if (mProductBitmap != null) {
                        mProductBitmap.recycle();
                    }
                    stream = getActivity().getContentResolver().openInputStream(
                            intent.getData());
                    mProductBitmap = BitmapFactory.decodeStream(stream);
                    System.out.println(mProductBitmap);
                    System.out.println("Setting image result");
                    imgProduct.setImageBitmap(mProductBitmap);
 
                catch (FileNotFoundException e) {
                    e.printStackTrace();
                finally {
                    if (stream != null)
                        try {
                            stream.close();
                        catch (IOException e2) {
                            e2.printStackTrace();
                        }
                }
            }
        }
    }

处理方法

你的图片已经报错到了 PATH_TO_SAVE  地址中

你需要在你的onActivityResult  方法中做么做
File file = new File(PATH_TO_SAVE);
Bitmap bmp = BitmapFactory.decodeFile(file.getPath());

原文地址:http://www.itmmd.com/201411/158.html 
该文章由 萌萌的IT人 整理发布,转载须标明出处。

抱歉!评论已关闭.