My app crash as soon as I select a image?

你。 提交于 2021-01-28 06:46:57

问题


I am completely new to Android Studio and I have been trying to make an app that scans using the (:scanLibrary) given by this link and followed the instructions given here https://github.com/jhansireddy/AndroidScannerDemo. There is no compilation error but only some runtime error. Can someone help me figure out why this is happening?

This is my code

 package com.mukesh.android.digiwrite;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.scanlibrary.ScanActivity;
import com.scanlibrary.ScanConstants;

import java.io.IOException;

public class MainActivity extends Activity {

private static final String TAG = "tag" ;
private Button click;
private Button gallery;
public final int request_Code = 99;
public ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "part2");
    click = (Button)findViewById(R.id.button);
    gallery = (Button)findViewById(R.id.button2);
    image = (ImageView)findViewById(R.id.imageView);

    gallery.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, ScanActivity.class);
            intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, ScanConstants.OPEN_MEDIA);
            startActivityForResult(intent, request_Code);
            Log.d(TAG, "part4");
        }
    });


   /* click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, ScanActivity.class);
            intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, ScanConstants.OPEN_CAMERA);
            startActivityForResult(intent, request_Code);
        }
    });*/

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "part5");
    if (requestCode == request_Code && resultCode == RESULT_OK) {
        Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
        Bitmap bitmap = null;
        Log.d(TAG, "part6");
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            getContentResolver().delete(uri, null, null);
            image.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


}

I have also given the required read and write permission in the manifest file as shown:

   <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mukesh.android.digiwrite">

<uses-permission android:name="android.permission.CAMERA"  />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera2.full" android:required="false" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

</manifest>

But still the app keeps getting closed as soon as I click on the gallery button and select an image. I tried running it on android sdk23.

Edit 1

This is the error I am getting.

 06-09 18:23:18.899 25835-25835/com.mukesh.android.digiwrite E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.mukesh.android.digiwrite, PID: 25835
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mukesh.android.digiwrite/com.scanlibrary.ScanActivity}: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.motorola.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/scanSample/IMG_20170609_182318.jpg} (has extras) } from ProcessRecord{1e3886 25835:com.mukesh.android.digiwrite/u0a131} (pid=25835, uid=10131) with revoked permission android.permission.CAMERA
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                           Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.motorola.camera/.Camera clip={text/uri-list U:file:///storage/emulated/0/scanSample/IMG_20170609_182318.jpg} (has extras) } from ProcessRecord{1e3886 25835:com.mukesh.android.digiwrite/u0a131} (pid=25835, uid=10131) with revoked permission android.permission.CAMERA
                                                                              at android.os.Parcel.readException(Parcel.java:1620)
                                                                              at android.os.Parcel.readException(Parcel.java:1573)
                                                                              at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2679)
                                                                              at android.app.Instrumentation.execStartActivity(Instrumentation.java:1693)
                                                                              at android.app.Activity.startActivityForResult(Activity.java:4570)
                                                                              at android.app.Activity.startActivityFromFragment(Activity.java:4556)
                                                                              at android.app.Activity$HostCallbacks.onStartActivityFromFragment(Activity.java:6652)
                                                                              at android.app.Fragment.startActivityForResult(Fragment.java:1112)
                                                                              at android.app.Fragment.startActivityForResult(Fragment.java:1101)
                                                                              at com.scanlibrary.PickImageFragment.openCamera(PickImageFragment.java:129)
                                                                              at com.scanlibrary.PickImageFragment.handleIntentPreference(PickImageFragment.java:77)
                                                                              at com.scanlibrary.PickImageFragment.init(PickImageFragment.java:58)
                                                                              at com.scanlibrary.PickImageFragment.onCreateView(PickImageFragment.java:48)
                                                                              at android.app.Fragment.performCreateView(Fragment.java:2220)
                                                                              at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
                                                                              at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                              at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                              at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1537)
                                                                              at android.app.FragmentController.execPendingActions(FragmentController.java:325)
                                                                              at android.app.Activity.performStart(Activity.java:6275)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:148) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
     06-09 18:23:20.589 26141-26153/? E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
     06-09 18:23:22.793 26217-26217/? E/NEW_BHD: Battery Power Supply logging Daemon start!!!!!
     06-09 18:23:22.794 26217-26217/? E/NEW_BHD: Cannot run on production devices!

My Xml file

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mukesh.android.digiwrite.MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="300dp"
    android:layout_height="300dp"
    app:srcCompat="@drawable/circle"
    android:layout_marginTop="50dp"
    android:layout_below="@+id/linearlayout"
    android:layout_centerHorizontal="true" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="bottom"
    android:gravity="center_horizontal"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:id="@+id/linearlayout">

    <Button
        android:id="@+id/button"
        android:layout_width="88dp"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:layout_margin="32dp"
        />




    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:text="@string/button2" />

</LinearLayout>


 </RelativeLayout>

I tried running it on android sdk 19 and it works fine. But in android sdk 23,24, 25 it isn't working.

Thank you


回答1:


You should follow this because android 6 introduced runtime permissions.

It is not enough to define needed permissions in android manifest, you have to ask explicitly to user.

If he doesn't authorize this (or you don't ask at all) your app will crash.

From official documentation:

Check for permissions

For example, this snippet shows how to check if the activity has permission to write to the calendar:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.WRITE_CALENDAR);

Request the permissions you need

The following code checks if the app has permission to read the user's contacts, and requests the permission if necessary:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}


来源:https://stackoverflow.com/questions/44457991/my-app-crash-as-soon-as-i-select-a-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!