Android application stops when using camera (onDestroy)

0

I have an android application which at a certain point the user requested a photographic evidence, before adding the services of google play services and firebase worked without problems .

The manifest is already configured in case it goes in the background keep the changes made.

<application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:hardwareAccelerated="true"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:name="android.support.multidex.MultiDexApplication"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <activity
            android:name=".ActivityLogin"
            android:configChanges="orientation|screenSize"
            android:icon="@drawable/logo"
            android:screenOrientation="portrait"
            android:multiprocess="true"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActivityMain"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:hardwareAccelerated="true"
            android:label="@string/app_name"
            android:multiprocess="true"
            android:screenOrientation="portrait" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>


        <!-- INSTALL PACKAGE
        <activity android:name=".PackageInstallerActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.INSTALL_PACKAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file" />
                <data android:mimeType="application/vnd.android.package-archive" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.INSTALL_PACKAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="content" />
                <data android:scheme="file" />
            </intent-filter>
        </activity>
        <activity android:name=".UninstallerActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:excludeFromRecents="true"
            android:theme="@android:style/Theme.DeviceDefault.Dialog.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.DELETE" />
                <action android:name="android.intent.action.UNINSTALL_PACKAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="package" />
            </intent-filter>
        </activity>
        INSTALL PACKAGE-->

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            android:multiprocess="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>
        <receiver android:name=".WifiReceiver">
            <intent-filter>
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
                <action android:name="android.net.wifi.STATE_CHANGE"/>
            </intent-filter>
        </receiver>
        <service
           android:name=".WifiReceiver$WifiActiveService"
           android:icon="@drawable/binoculars"
           android:label="LecturasService" />

        <!--<providers
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>-->

    </application>

The added services are the following:

build.gradle

.....
    dependencies {
            classpath 'com.android.tools.build:gradle:2.3.3'

            classpath 'com.google.gms:google-services:3.1.1'
            classpath 'com.google.firebase:firebase-plugins:1.1.1'
        }
.....
    allprojects {
        repositories {
            jcenter()
            maven { url "https://maven.google.com" }
        }
    }
....

app > build.gradle

...
dependencies {

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile 'com.melnykov:floatingactionbutton:1.3.0'
    compile 'com.getbase:floatingactionbutton:1.10.1'
    /*Retrofit JSON Parsing*/
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
    compile 'com.google.code.gson:gson:2.8.1'
    /*Play Services*/
    compile 'com.google.android.gms:play-services:11.4.2'
    compile 'com.google.gms:google-services:3.1.1'
    /*Firebase Crash*/
    compile 'com.google.firebase:firebase-crash:11.4.2'
    compile 'com.google.firebase:firebase-messaging:11.4.2'
    //compile 'com.google.firebase:firebase-perf:11.2.2'
    //noinspection GradleCompatible
    compile 'com.android.support:multidex:1.0.1'
    /*Otros*/
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support:support-v4:26.0.0-alpha1'
    compile 'com.android.support:support-vector-drawable:26.0.0-alpha1'
}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.firebase-crash'

Method that I use to run or request the camera.

public void intentTakePhoto(int codeResult)
    {
        boolean result = Permission.checkPermissionCamera(ActivityMain.this);
        Log.e("intentTakePhoto: ", " = "+result);
        if (result)
        {
            if(contrato.getCount()>0)
            {
                imageName = "/DIRAPP/" + pref.getText() + "_" + pos.getSelectedItem().toString() + "_" + codeResult + ".jpg";
                file = new File(Environment.getExternalStorageDirectory() + imageName);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                Uri Foto = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName()+ ".provider", file);

                if (Build.VERSION.SDK_INT > 23)
                {
                    Log.e("intentTakePhoto: ", ">21");
                    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Foto);
                }else
                {
                    Log.e("intentTakePhoto: ", "Normal");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                }
                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("stock",stock.getText().toString());
                editor.commit();
                startActivityForResult(intent, codeResult);
            }else{
                mostrarAlerta("No hay datos.");
            }
        }else{
            mostrarAlerta("No tiene permisos para usar la camara");
        }
    }

After taking the photographic evidence, or when opening the camera the application restarts.

    
asked by Desarrollador Android Jr. 25.10.2017 в 00:14
source

1 answer

0

It is possible that the problem is in the Environment.getExternalStorageDirectory () method, since the directory might not be available at the time of execution.

To check the state of the directory you can use the Environment.getExternalStorageState () method that will return a String with the state of the external storage and that can be one of these values (MEDIA_UNKNOWN, MEDIA_REMOVED, MEDIA_UNMOUNTED, MEDIA_CHECKING, MEDIA_NOFS, MEDIA_MOUNTED, MEDIA_MOUNTED_READ_ONLY, MEDIA_SHARED, MEDIA_BAD_REMOVAL, or MEDIA_UNMOUNTABLE)

Additionally, getExternalStorageDirectory () requires the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions.

If you are using a minimum API higher than KITKAT you can use instead getExternalFilesDir (String) or getExternalCacheDir () since they do not require write or read permission.

I hope it helps you.

    
answered by 26.10.2017 в 19:08