Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.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 fragment中从相机拍照不起作用_Android_Camera_Fragment - Fatal编程技术网

在android fragment中从相机拍照不起作用

在android fragment中从相机拍照不起作用,android,camera,fragment,Android,Camera,Fragment,我可以从画廊中拍照,但在从相机拍摄的片段中发现了问题。 在ActivityResult上拍摄照片后,有时会调用它,当调用它时,会给出一些找不到的异常文件 我的代码是 if (requestCode == CAMERA_REQUEST && resultCode == getActivity().RESULT_OK) { if(flag==0){ try{ String URI = getImageURI();

我可以从画廊中拍照,但在从相机拍摄的片段中发现了问题。 在ActivityResult上拍摄照片后,有时会调用它,当调用它时,会给出一些找不到的异常文件

我的代码是

if (requestCode == CAMERA_REQUEST && resultCode == getActivity().RESULT_OK) {
if(flag==0){
    try{                   
       String URI = getImageURI();
       String imageName = URI.substring(URI.lastIndexOf("/")+1);
       FileInputStream fis = mContext.openFileInput(imageName);
       Bitmap photo = BitmapFactory.decodeStream(fis);
       Matrix matrix = new Matrix();
       matrix.preRotate(90);
       photo = Bitmap.createBitmap(photo , 0, 0, photo.getWidth(),    photo.getHeight(), matrix, true);
    }
    catch(Exception e){ 
            Log.e("Error - ",e.getMessage());
    }
   }
 }

 public void takePictureFromCamera(){
              File style = new File(Environment.getExternalStorageDirectory(),"style");
      if(!style.exists()){style.mkdir();}
        String d = System.currentTimeMillis()+"";
        File f = new File(style, d+"style.jpg");
        absPath = f.getAbsolutePath();
        savePref(absPath);
        cameraImagePath = Uri.fromFile(f);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImagePath);
        }
        getActivity().startActivityForResult(takePictureIntent, CAMERA_REQUEST);
  }

以下是我如何处理onActivityResult方法问题的步骤

我有一个主要活动和一个子片段。 现在我的片段的onActivityResult没有被调用,而是调用MainActivity的(super)onActivityResult。 下面是让它工作的示例代码片段

  • 我已经在我的主要活动中重写了这个“onActivityResult”,如果需要,我将从这里调用子片段的onActivityResult方法

    受保护的void onActivityResult(int请求代码、int结果代码、意图数据)

  • “内容框架”是我的主要活动的XML文件中的框架布局

    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    

    1.对于您正在测试的设备上的“file now found error”,即Nexus不支持存储卡,因此无法使用getExternalStorageDirectory方法获取,或者第二个原因是您没有在清单2中添加写入权限。对于onActivityResult,没有被调用的原因是因为它调用它的超级活动的onActivityResult,所以您可能需要从那里调用子片段的方法。Thanks作为您的答案,我正在三星galaxy s4上测试它,并在清单中添加了权限。你提到的调用子片段方法时,我必须从onActivityResult调用哪个方法。在我的活动中添加onActivityResult,在我的代码中修改后,它对我有效。谢谢你救了我的命。
    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    ProfileFragment fragment = new ProfileFragment();
       Bundle args2 = new Bundle();
       args2.putInt(ActivityFeedFragment.ARG_NAVIGATIN_LIST_NUMBER, position);
       args2.putString(Utils.serfrmuserid, AssetPref.getString(Utils.User_ID, null));
       fragment.setArguments(args2);
    
       FragmentManager fragmentManager2 = getFragmentManager();
       fragmentManager2.beginTransaction().replace(R.id.content_frame, fragment).commit();