简体   繁体   English

在android片段中从相机拍照无法正常工作

[英]Taking picture from camera in android fragment is not working

I am able to take picture from gallery but found problem in capture from camera in fragment. 我可以从画廊拍照,但在从片段中捕获相机时发现问题。 After picture taken onActivityResult is some times called and when called it gives some exception file not found. 拍照后,onActivityResult会被调用几次,调用时会提供一些找不到的异常文件。

my code is 我的代码是

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);
  }

Here is the steps how I am handling onActivityResult method issue. 这是我处理onActivityResult方法问题的步骤。

I have One MainActivity and one child Fragment. 我有一个MainActivity和一个孩子Fragment。 now my Fragment's onActivityResult is not getting called, instead it calls MainActivity's (super) onActivityResult. 现在我的Fragment的onActivityResult没有被调用,而是调用了MainActivity的(超级)onActivityResult。 So below is the sample code snip to get it work 所以下面是使它工作的示例代码片段

  1. This "onActivityResult" i have overridden in my main activity and from here I am calling child fragment's onActivityResult method if needed. 我已经在主要活动中覆盖了此“ onActivityResult”,如果需要的话,从这里我将调用子片段的onActivityResult方法。

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 受保护的void onActivityResult(int requestCode,int resultCode,Intent数据)

     { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE: { Fragment fragment = getFragmentManager().findFragmentById(R.id.content_frame); fragment.onActivityResult(requestCode, resultCode, data); } break; default: break; } } 

"content_frame" is Frame layout inside my Main Activity's XML file “ content_frame”是我的主活动的XML文件中的框架布局

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

I start, my profile fragment this way in MainActivity class 我开始,在MainActivity类中以这种方式创建个人资料片段

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM