Question

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.

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);
  }
Was it helpful?

Solution

Here is the steps how I am handling onActivityResult method issue.

I have One MainActivity and one child Fragment. now my Fragment's onActivityResult is not getting called, instead it calls MainActivity's (super) 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.

    protected void onActivityResult(int requestCode, int resultCode, Intent data)

    {
        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

<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

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top