0

I am working with capturing an image and then show it on my ImageView in my fragment. I searched this through web and still I cannot make it. Below is what I am doing.

btnCaptureImg.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(cameraIntent, 1888); 
    }
});

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e("LOG", ""+requestCode);
    if (requestCode == 1888 && resultCode == Activity.RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }

    super.onActivityResult(requestCode, resultCode, data);
}  

The Logcat says 1888 only but the imageView did not load the image.

I also tried this

btnCaptureImg.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        File folder = new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/");
        folder.mkdirs();

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        resultingFile = new File(folder.toString() + "/image.jpg");
        Uri uriSavedImage=Uri.fromFile(resultingFile);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

        startActivityForResult(cameraIntent, 1888);
    }
});

But it throws exception:

E/AndroidRuntime(4824): java.lang.RuntimeException: Unable to resume activity {com.myapp.android/com.myapp.android.MyHomeActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=264032, result=-1, data=null} to activity {com.myapp.android/com.myapp.android.MyHomeActivity}: java.lang.NullPointerException
E/AndroidRuntime(4824):     at com.myapp.android.MyFragment.onActivityResult(MyFragment.java:300)
kads
  • 112
  • 1
  • 10
  • Take into account that `onActivityResult` is not called in `Fragment` but in `Activity`. – Héctor Sep 03 '14 at 08:57
  • So how can I achieve my goal which is to take a picture and display it to ImageView using fragment? Is it possible? – kads Sep 03 '14 at 09:17

3 Answers3

0

Check if your onActicityResult is beeing called. If not check: onActivityResult is not being called in Fragment

Community
  • 1
  • 1
aldorain
  • 790
  • 5
  • 16
  • Yes, it is being called in my first approach. It logged me the requestCode which is 1888. – kads Sep 03 '14 at 09:15
0

Try putting

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, your_image_uri);

before this

startActivityForResult(cameraIntent, 1888);

I'm sorry mate but I can't seem to understand why it's giving that exception. Try this approach, it works for me.

Define the file globally and inside onActivityResult

    if (requestCode == 1888 && resultCode == Activity.RESULT_OK) { 
        Bitmap  photo = Media.getBitmap(getActivity().getContentResolver(), Uri.fromFile(resultingFile)); 
        //Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }
Thahzan
  • 975
  • 2
  • 9
  • 23
  • I did that already as to what this [link](http://stackoverflow.com/questions/14187146/emulator-crashes-when-capturing-a-photo-nullpointerexception) has stated. But it throws an exception. See my updated question. – kads Sep 03 '14 at 09:15
  • Bitmap photo = (Bitmap) data.getExtras().get("data"); – kads Sep 03 '14 at 09:31
  • I can't seem to understand why it's giving that exception. Try the edited answer. – Thahzan Sep 03 '14 at 09:43
  • Thank you @Thahzan. There's no exception now. BUT I put Log inside this condition if (requestCode == 1888 && resultCode == Activity.RESULT_OK) { Log.e("IMAGE", "CHANGING"); Bitmap photo = (Bitmap) data.getExtras().get("data"); imgAddImg.setImageBitmap(photo); } But the imageView did not load the expected bitmap. What's wrong with it? Is it because the fragment refreshed after taking a picture? Please bare with me, I am new to use this Camera API of android. – kads Sep 03 '14 at 09:59
  • I'm no veteran on Android either mate. Check if the captured image is stored in the correct path. If it's there, we can safely say everything until that part is working without any issues. And are you testing this on a Samsung device? I ran into a NullPointerException when using this method to take images from some Samsung tabs. I had to declare everything outside onActivityResult to get around it (which is a terrible idea btw). – Thahzan Sep 03 '14 at 10:15
  • Yes. I do use samsung tab as my test device. I will test this on other device. Thanks. – kads Sep 03 '14 at 10:17
0

First, in the fragment starts the intent to capture the image:

private void startIntentImageCapture() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile("imageName.jpg", ".jpg", storageDir);
        path = Uri.fromFile(image);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, path);
        getActivity().startActivityForResult(cameraIntent, CAPTURE_IMAGE_REQUEST_CODE);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

path variable, must be an public static Uri instance. And another public static Integer for the request code: public static final Integer CAPTURE_IMAGE_REQUEST_CODE = 100;

Then in the Activity, read this path and decode the image:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == YourFragment.CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Bitmap imageBitmap = decodeFile(YourFragment.path);
                //do something with your photo
            }
        }
    }

I'm sure this is not the best and cleaner method but it works for me. Hope it helps!

EDIT

I forgot the code to decode the image.

 public Bitmap decodeFile(Uri uriFile) throws OutOfMemoryError {
        String filePath = uriFile.getPath();
        BitmapFactory.Options bmOptions;
        Bitmap imageBitmap;
        try {
            imageBitmap = BitmapFactory.decodeFile(filePath);
        } catch (OutOfMemoryError e) {
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 4;
            bmOptions.inPurgeable = true;
            imageBitmap = BitmapFactory.decodeFile(filePath, bmOptions);
        }
        return imageBitmap;
    }

The bmOptions ensure that OutOfMemoryError doesn't occurs.

Héctor
  • 24,444
  • 35
  • 132
  • 243