2

I've already read a lot of posts, but none of them have a similar situation.

Context

The following code works fine for API 17 - 22, but not for API 23. The code below is inside the fragment.

private void takePicture() {

    // identify if the device has camera to decide the next flow of action
    PackageManager pm = mContext.getPackageManager();
    if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Toast.makeText(mContext, R.string.msgPleaseTakePicture2, Toast.LENGTH_LONG).show();
        dispatchTakePictureIntent();
    }else {
        showErrorDialog(getString(R.string.msgErrorPicture));
    }

}

private void dispatchTakePictureIntent() {

    ContentValues values;
    values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "Doarnf");
    values.put(MediaStore.Images.Media.DESCRIPTION, "Doarnf");
    imageUri = getActivity().getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

}

//on ActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    switch (requestCode)
    {
        case REQUEST_IMAGE_CAPTURE:
            getPictureResults(resultCode, intent);
            break;
    }

}

private void getPictureResults(int resultCode, Intent intent) {

    if (resultCode == Activity.RESULT_OK)
    {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
            mImageBitmap = ImageHelper.scaleBitmapBasedOnHeight(mImageBitmap, 1296);
            executeDonation();

        } catch (Exception e) {
            showErrorDialog(getString(R.string.msgErrorPicture));
        }
    }else {
        showErrorDialog(getString(R.string.msgErrorPicture));
    }

}

AndroidManifest.xml

<activity
    android:name="br.com.cfb.doarnf.NavigationDrawerActivity"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:theme="@style/AppDrawer"
    android:windowSoftInputMode="adjustPan"
    android:screenOrientation="portrait">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="br.com.cfb.doarnf.MainActivity" />
</activity>

The Problem

The problem is:

  • Sometimes the onActivityResults is called back but the fragment variables are lost.

  • Sometimes the OnActivityResults is not called back. When the picture is taken the Android main screen shows up.

So I'm supposing that it is a bug from Android API 23.

Any idea?

Thank you in Advance.

7geeky
  • 438
  • 1
  • 12
  • 26
Carlos
  • 433
  • 5
  • 19

1 Answers1

0

For API level 23 and above permissions from Menifest file is not enough, you need to ask for permission externally when you need, so I hope you have wrote permissions like READ_EXTERNAL_STORAGE and all in menifest file, but for API 23 and above it is required externally ,

So for that apply external permission and you are not aware for this then check my below answer :

issues in writing files to sd card in android api 23(marshmallow)

Community
  • 1
  • 1
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34