0

I have a simple fragment that allows the user to take a picture, save it to their gallery and output it in an imageView. It works fine when I just run it as an activity but when I converted it to a fragment then the image doesn't appear in the image view, any idea what I'm doing wrong?

Home.java

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

public class Home extends Fragment implements View.OnClickListener {

/** Called when the activity is first created. */

private final int CAMERA_RESULT = 1;

private final String Tag = getClass().getName();

Button button1;

ImageView imageView1;

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_home, container, false);
    super.onCreate(savedInstanceState);



    button1 = (Button)rootView.findViewById(R.id.button1);

    imageView1 = (ImageView)rootView.findViewById(R.id.imageView1);

    button1.setOnClickListener(this);

    return rootView;

}

public void onClick(View v) {

    PackageManager pm = getActivity().getPackageManager();

    if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);

        startActivityForResult(i, CAMERA_RESULT);

    } else {

        Toast.makeText(getActivity().getBaseContext(), "Camera is not available", Toast.LENGTH_LONG).show();

    }   }



@Override

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

    super.onActivityResult(requestCode, resultCode, data);

    Log.i(Tag, "Receive the camera result");

    if (resultCode == getActivity().RESULT_OK && requestCode == CAMERA_RESULT) {

        File out = new File(getActivity().getFilesDir(), "newImage.jpg");
        Toast.makeText(getActivity().getBaseContext(),

                "Image captured and stored successfully", Toast.LENGTH_LONG)

                .show();

        if(!out.exists()) {

            Toast.makeText(getActivity().getBaseContext(),

                    "Error while capturing image", Toast.LENGTH_LONG)

                    .show();

            return;

        }

        Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());

        imageView1.setImageBitmap(mBitmap);

    }

}



@Override

public void onDestroy() {

    super.onDestroy();

    imageView1 = null;

}

}

MyFileContentProvider class

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;

public class MyFileContentProvider extends ContentProvider {

public static final Uri CONTENT_URI = Uri.parse("content://com.example.camerademo/");

private static final HashMap<String, String> MIME_TYPES = new HashMap<String, String>();

static {

    MIME_TYPES.put(".jpg", "image/jpeg");

    MIME_TYPES.put(".jpeg", "image/jpeg");

}

@Override

public boolean onCreate() {

    try {

        File mFile = new File(getContext().getFilesDir(), "newImage.jpg");

        if(!mFile.exists()) {

            mFile.createNewFile();

        }

        getContext().getContentResolver().notifyChange(CONTENT_URI, null);

        return (true);

    } catch (Exception e) {

        e.printStackTrace();

        return false;

    }

}

@Override

public String getType(Uri uri) {

    String path = uri.toString();

    for (String extension : MIME_TYPES.keySet()) {

        if (path.endsWith(extension)) {

            return (MIME_TYPES.get(extension));

        }

    }

    return (null);

}

@Override

public ParcelFileDescriptor openFile(Uri uri, String mode)

        throws FileNotFoundException {

    File f = new File(getContext().getFilesDir(), "newImage.jpg");

    if (f.exists()) {

        return (ParcelFileDescriptor.open(f,

                ParcelFileDescriptor.MODE_READ_WRITE));

    }

    throw new FileNotFoundException(uri.getPath());

}

@Override

public Cursor query(Uri url, String[] projection, String selection,

                    String[] selectionArgs, String sort) {

    throw new RuntimeException("Operation not supported");

}

@Override

public Uri insert(Uri uri, ContentValues initialValues) {

    throw new RuntimeException("Operation not supported");

}

@Override

public int update(Uri uri, ContentValues values, String where,

                  String[] whereArgs) {

    throw new RuntimeException("Operation not supported");

}

@Override

public int delete(Uri uri, String where, String[] whereArgs) {

    throw new RuntimeException("Operation not supported");

}

}

1 Answer 1

0

I solved it, silly mistake that I didn't notice, was using the wrong package name in the contentProvider

public static final Uri CONTENT_URI = Uri.parse("content://com.example.camerademo/");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.