从gallery加载图像时无法加载空位图

d8tt03nd  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(358)

我试图从相机和画廊加载图像加载内部tensorflow lite模型。从图库加载图像时,出现以下错误。

2021-01-02 11:48:03.309 9858-9858/com.example.coco_classif E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/20200714_135735 (1).jpg: open failed: EACCES (Permission denied)
2021-01-02 11:48:03.332 9858-9858/com.example.coco_classif E/Error log: java.lang.NullPointerException: Cannot load null bitmap.

注意:问题似乎来自try-catch块,从相机加载工作正常
维护

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.coco_classif">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.WRITE_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/Theme.Coco_classif">
        <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>

主要活动

package com.example.coco_classif;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private static final int CAMERA_PERMISSION_REQUEST_CODE = 1000;
    private static final int CAMERA_REQUEST_CODE = 10001;
    private ImageView imageView;
    private ListView listView;
    private ImageClassifier imageClassifier;

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

        inaializeUIElements(); 
    }

    private void inaializeUIElements() {
        imageView = findViewById(R.id.iv_capture);
        listView = findViewById(R.id.lv_probabilities);
        Button takepicture = findViewById(R.id.bt_take_picture);

        try {
            imageClassifier = new ImageClassifier(this);
        } catch (IOException e) {
            Log.e("Error While Creating Image Classifier", "Error" +e);
        }

        takepicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(hasPermission()) {
                    selectImage();
                }else{
                    requestPermission();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                Bitmap photo = (Bitmap) Objects.requireNonNull(Objects.requireNonNull(data).getExtras()).get("data");
            imageView.setImageBitmap(photo);

            List<ImageClassifier.Recognition> predictions = imageClassifier.recognizeImage(photo, 0);

            final List<String> predictionList = new ArrayList<>();
            for(ImageClassifier.Recognition recog: predictions){
                predictionList.add("Label: " + recog.getName() + "Confidence: " + recog.getConfidance());
            }

            ArrayAdapter<String> predictionsAdapter = new ArrayAdapter<>(
                    this,R.layout.support_simple_spinner_dropdown_item,predictionList);

            listView.setAdapter(predictionsAdapter);

        }

            else if (requestCode == 2) {
                try {
                    Uri selectedImage = data.getData();
                    String[] filePath = new String[]{MediaStore.Images.Media.DATA};
                    Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
                    c.moveToFirst();
                    int columnIndex = c.getColumnIndex(filePath[0]);
                    String picturePath = c.getString(columnIndex);
                    c.close();
                    Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                    Log.w("path of image from gallery......******************.........", picturePath+"");
                    Toast.makeText(this,picturePath.toString(),Toast.LENGTH_LONG).show();

                    imageView.setImageBitmap(thumbnail);

                    List<ImageClassifier.Recognition> predictions = imageClassifier.recognizeImage(thumbnail, 0);

                    final List<String> predictionList = new ArrayList<>();
                    for(ImageClassifier.Recognition recog: predictions){
                        predictionList.add("Label: " + recog.getName() + "Confidence: " + recog.getConfidance());
                    }

                    ArrayAdapter<String> predictionsAdapter = new ArrayAdapter<>(
                            this,R.layout.support_simple_spinner_dropdown_item,predictionList);

                    listView.setAdapter(predictionsAdapter);

                }catch (Exception e){

                    Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
                    Log.e("Error log", e.toString());
                }

            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(hasAllPermissions(grantResults)){
            openCamera();
        }else {
            requestPermission();
        }
    }

    private boolean hasAllPermissions(int[] grantResults) {
        for(int result : grantResults){
            if (result == PackageManager.PERMISSION_DENIED)
                return false;
        }
        return true;
    }

    private void requestPermission() {
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)){
                Toast.makeText(this," Permission required",Toast.LENGTH_SHORT).show();
            }
            requestPermissions(new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
        }
    }

    private void openCamera() {
        Intent camerIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(camerIntent,CAMERA_REQUEST_CODE);
    }

    private boolean hasPermission() {
       if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
           return checkSelfPermission(Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED;
       }
       return true;
    }

    private void selectImage() {
        final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Take Photo")) {
                    Intent camerIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(camerIntent,1);
                } else if (options[item].equals("Choose from Gallery")) {
                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 2);

                } else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }
    }
vd2z7a6w

vd2z7a6w1#

注意:这个代码片段只适用于编写文本文件,我使用这个方法来解决open failed:eacces(权限被拒绝)
我知道您的实现必须处理图像,这导致了2021-01-02 11:48:03.332 9858-9858/com.example.coco\u classif e/error log:java.lang.nullpointerexception:无法加载空位图。
另外,在android上检查这个异常“openfailed:eacces(permission denied)”
我遇到了完全相同的问题,我所做的只是创建一个目录并在其中编写一个文本文件。
另外,以编程方式为图像创建新文件名是一种很好的做法,使用日期格式化程序。
我可以看到您正在使用警报对话框来选择图像,我建议您不要这样做,警报对话框会导致很多同步问题。
这段代码在androidqone+6t上进行了测试,没有任何错误。

public void generateNoteOnSD(Context context, String sFileName, String sBody) {
    try {
        File root = new File(String.valueOf(Environment.getExternalStorageDirectory())+"/sysInfoReports");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

检查您的权限:旧的存储权限在android r上不起作用。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

相关问题