我选择图片后,我的应用程序崩溃了吗?

时间:2017-06-09 12:33:47

标签: android android-intent android-activity android-camera android-gallery

我是Android Studio的新手,我一直在努力创建一个使用此链接提供的(:scanLibrary)进行扫描的应用,并按照此处https://github.com/jhansireddy/AndroidScannerDemo给出的说明进行操作。没有编译错误,只有一些运行时错误。有人可以帮我弄清楚为什么会这样吗?

这是我的代码

 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();
        }
    }
}


}

我还在清单文件中给出了所需的读写权限,如下所示:

   <?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>

但是,只要点击图库按钮并选择图像,应用就会一直关闭。我尝试在android sdk23上运行它。

编辑1

这是我得到的错误。

 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!

我的Xml文件

  <?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>

我尝试在android sdk 19上运行它,它运行正常。但是在android sdk 23,24,25中它没有用。

谢谢

1 个答案:

答案 0 :(得分:0)

你应该遵循this,因为android 6引入了运行时权限。

在android清单中定义所需的权限是不够的,你必须明确地向用户询问。

如果他没有授权(或者你根本不问)你的应用会崩溃。

来自官方文档:

Check for permissions

例如,此代码段显示了如何检查活动是否有权写入日历:

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

Request the permissions you need

以下代码检查应用是否有权读取用户的联系人,并在必要时请求权限:

// 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.
    }
}