18

I have used Navigation drawer in each item click i have called Fragments so in one item i have called one Fragment in this fragment i need to get picture from camera and set it to as canvas background. In this I have captured camera picture but don't know how to get this picture after captured and set it to on canvas background.

Fragment code

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ssoft.admin.code.SharedPreferenceStore;
import com.ssoft.admin.code.Tools;
import com.ssoft.admin.salesmateco.FragSiteInspectionAdditional;
import com.ssoft.admin.salesmateco.R;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FragSignature extends Fragment implements View.OnClickListener {
    Button mSIBtnCamera;
    Fragment fragment;
    Tools mTools;
    private static final int RESULT_OK = 1;
    private static final int RESULT_CANCELED = 0;
    Uri imageUri = null;
    final int CAMERA_DATA = 100, INTENT_DATA = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.frag_site_inspection_signature, null);
        mSIBtnCamera = (Button) rootView.findViewById(R.id.camera);
        mSIBtnCamera.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.camera) {
            captureImage();
        }

        else {
            Toast.makeText(getActivity().getApplicationContext(),
                    "FragSIPhotos Add Button OnClick", Toast.LENGTH_SHORT)
                    .show();

        }
    }

    public void captureImage() {
        // Define the file-name to save photo taken by Camera activity

        String fileName = "Images.jpg";

        // Create parameters for Intent with filename

        ContentValues values = new ContentValues();

        values.put(MediaStore.Images.Media.TITLE, fileName);

        values.put(MediaStore.Images.Media.DESCRIPTION,
                "Image capture by camera");

        // imageUri is the current activity attribute, define and save it for
        // later usage

        Uri imageUri = getActivity().getApplicationContext()
                .getContentResolver()
                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        /****
         * EXTERNAL_CONTENT_URI : style URI for the "primary" external storage
         * volume.
         ****/

        // Standard Intent action that can be sent to have the camera
        // application capture an image and return it.

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, INTENT_DATA);
        Log.e("captureImage()", "state -1");
        getActivity().startActivityForResult(intent, CAMERA_DATA);
        Log.e("captureImage()", "end");

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("OnActivityResult()", "1");

        if (requestCode == CAMERA_DATA) {
            Log.e("OnActivityResult()", "2");

            if (resultCode == RESULT_OK) {
                // Image captured and saved to fileUri specified in the Intent
                Log.e("OnActivityResult()", "3");
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
                Log.e("OnActivityResult()", "4");
            } else {
                // Image capture failed, advise user
                Log.e("OnActivityResult()", "5");
            }
        }
        Log.e("OnActivityResult()", "6");
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("OnActivityResult()", "7");
    }

}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Hiren Vaghela
  • 916
  • 1
  • 9
  • 22
  • 1
    read this http://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity – upenpat Feb 11 '15 at 08:54
  • but in my project cursor not go in onActivityResult() method. – Hiren Vaghela Feb 11 '15 at 09:18
  • Ensure that you have overridden onActivityResult in your fragment's parent Activity and make a call to `super.onActivityResult(requestCode,resultCode,data);` in the activity. If you do not do this, the fragments onActivityResult() never gets called. – Shivam Verma Feb 11 '15 at 09:52
  • possible duplicate of [onActivityResult not being called in Fragment](http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment) – Lokesh Feb 18 '15 at 09:00

3 Answers3

39

In Activity class:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{

    super.onActivityResult(requestCode,resultCode,data);

}

In Fragment :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){}

Option 1 :

If you're calling startActivityForResult() from the fragment then you should call startActivityForResult() not getActivity().startActivityForResult(), as it will result in fragment onActivityResult().

If you're not sure where you're calling on startActivityForResult() and how you will be calling methods.

Option 2:

Since Activity gets the result of onActivityResult(), you will need to override the activity's onActivityResult() and call super.onActivityResult()to propagate to the respective fragment for unhandled results codes or for all.

If above 2 options do not work, then refer option 3 as it will definitely work.

Option 3 :

Explicit call from fragment to onActivityResult function as follows

In Parent Activity class, override the onActivityResult() method and even override the same in Fragment Class and call as the following code.

In Activity:

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment"); 
fragment.onActivityResult(requestCode, resultCode, data); 
}

In Fragment:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   //in fragment class callback
}
Psypher
  • 10,717
  • 12
  • 59
  • 83
Arslan Sohail
  • 1,583
  • 1
  • 12
  • 18
  • Thanks for your suggestion but i have used ActionBarActivity not used FragmentActivity so i will try your suggestion but it also throws NullPointerException. – Hiren Vaghela Feb 12 '15 at 04:55
  • you need to call StartActivityForResult() instead of getActivity().startActivityforResult() in your Fragment for the above solution to work fine ,let me know if you are still facing this issue. – Arslan Sohail Feb 12 '15 at 06:15
  • Please see this thread,hopefully it will solve your issue http://stackoverflow.com/questions/6147884/onactivityresult-not-being-called-in-fragment – Arslan Sohail Feb 12 '15 at 10:32
  • in my Fragment camera start and get picture but in onActivityResult Intent parameter is null value return how to get Value in Intent in the fragment can't understand... and mr.Arslan your link is good i have check all the links but in this link can't find my solution. in my mainActivity i have used ActionBarActivity and ExpandableNavigationDrawer. – Hiren Vaghela Feb 12 '15 at 11:38
  • Good one. Thanks Bro! We can meet if you are in Lahore ;) – Faizan Mubasher Feb 04 '18 at 07:19
6

Replace

getActivity().startActivityForResult(intent, CAMERA_DATA);

with

startActivityForResult(intent, CAMERA_DATA);
Pang
  • 9,564
  • 146
  • 81
  • 122
Ramesh
  • 526
  • 3
  • 14
0

onActivityResult should be implemented in Activity, this class is your Fragment.
Apply onActivityResult inside FragmentActivity.

Bhavana Vadodariya
  • 2,287
  • 1
  • 19
  • 26
  • i have no FragmentActivity i have ActionBarActivity in which i have used ExpandableNavigationDrawer in Navigation drawer one sub item open this Fragment and get Picture and stored. – Hiren Vaghela Feb 12 '15 at 12:33
  • Use activity, any kind of activity you are using. You can implement inside ActionBarActivity. – Bhavana Vadodariya Feb 19 '15 at 07:05