java - 无法解码流: java. io.FileNotFoundException :/storage/emulated/0/image. jpg:打开失败:EACCES(权限被拒绝)

标签 java android

我不断收到此错误。我不知道为什么,我检查了我的权限

  04-20 19:35:19.556  12158-12158/com.example.matts.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
04-20 19:35:19.586  12158-12158/com.example.matts.myapplication I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@420d5168 time:146616646
04-20 19:35:22.006  12158-12158/com.example.matts.myapplication I/Timeline﹕ Timeline: Activity_launch_request id:com.example.matts.myapplication time:146619061
04-20 19:35:22.086  12158-12158/com.example.matts.myapplication I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42206588 time:146619146
04-20 19:35:27.196  12158-12158/com.example.matts.myapplication D/AndroidRuntime﹕ Shutting down VM
04-20 19:35:27.196  12158-12158/com.example.matts.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41598db8)
04-20 19:35:27.196  12158-12158/com.example.matts.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.matts.myapplication, PID: 12158
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.matts.myapplication/com.example.matts.myapplication.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3389)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3432)
            at android.app.ActivityThread.access$1300(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.matts.myapplication.MainActivity.onActivityResult(MainActivity.java:47)
            at android.app.Activity.dispatchActivityResult(Activity.java:5423)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3385)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3432)
            at android.app.ActivityThread.access$1300(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at dalvik.system.NativeStart.main(Native Method)

这是代码。我想做的是拍照,将照片保存在 tmp 位置,然后将图片加载到位图 ImageView 中。

代码:

package com.example.matts.myapplication;

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

import java.io.File;


public class MainActivity extends Activity {

    static final int REQUEST_IMAGE_CAPTURE = 1;
    private ImageView imageView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        openCamera();
        setContentView(R.layout.activity_main);
        isExternalStorageWritable();
    }

    public void openCamera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "image.jpg");
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }



    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        //Check that request code matches ours:
        if (requestCode == REQUEST_IMAGE_CAPTURE){
            //Get our saved file into a bitmap object:
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
            Bitmap image = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
            imageView.setImageBitmap(image);
        }
    }

    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
    { // BEST QUALITY MATCH

        //First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize, Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight)
        {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }
        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth)
        {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }

        options.inSampleSize = inSampleSize;

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path, options);
    }

    public void capture_btn(View v) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

Android list

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



    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >



        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >

        </activity>

        <!-- Splash screen -->
        <activity
            android:name=".SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

最佳答案

将这些行移到 <application> 之外(上方)标签。

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

关于java - 无法解码流: java. io.FileNotFoundException :/storage/emulated/0/image. jpg:打开失败:EACCES(权限被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755502/

相关文章:

android - 当应用关闭时,就像在VLC Player中一样,在后台(在通知栏中)播放视频声音(在通知栏中)

android - IntentService 有时不被调用

java - Android 通过 java 代码更改 XML 文件的背景

android - ScrollView 下方的线性布局未显示

java - 如何设置 Mule JPA 模块以使用 Hibernate JPA 实现?

java - 我应该从 Applets 迁移出去吗?如果是,迁移到什么地方?

java - Java 是如何逐步评估这样的东西的?

java - 当 RequestBody 参数的某些属性为 null 时,如何返回 400 HTTP 错误代码?

java - Android - 在运行任务时更新 UI?

java - 将自定义 Java 数据模型传递到我的 native 代码