1

enter image description hereIf I use ContentProvider in my app am getting error like "unfortunately camera has stopped" after Result_ok.This is my code:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
 startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

How to to solve this?I don't want to save image in sd card.

This my MyFileContentProvider class:

public class MyFileContentProvider extends ContentProvider {

    public static final Uri CONTENT_URI = Uri.parse("content://com.example.user.studentadmission/");
    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(), "student.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(), "student.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");
    }

} 
Amshu
  • 127
  • 10
  • Post your logcat message. That would be helpful to understand the problem. – ajantha Sep 16 '16 at 05:06
  • @Amshu I suggest you to go through [this](https://github.com/google/cameraview) example, its a good one. – Nisarg Sep 16 '16 at 05:09
  • @ajantha I am not getting any messages in logcat. – Amshu Sep 16 '16 at 05:10
  • Are u using intent to capture image only? and what is contect provider working here. – W4R10CK Sep 16 '16 at 05:15
  • @W4R10CK worked on some devices.In one mobile I was getting this error.I am using intent to capture image only. I have edited my question and included class MyFileContentProvider now. – Amshu Sep 16 '16 at 05:17

2 Answers2

2

Few camera action works only on Kitkat, use condition for action:

Intent i = (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
                        ? new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)
                        : new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

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

If still crash occur, try avoiding ContentProvider just for testing. Let me know.

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
0

Give permission for open Camera. Give run-time permission for open Camera.

Hardik Vaghasiya
  • 298
  • 1
  • 3
  • 11