存储权限错误访问被拒绝

时间:2016-05-19 17:07:23

标签: java android ocr tesseract

我在sdCard上创建文件夹时出现存储权限错误。

日志

05-19 18:02:24.927 21214-21214/com.fortytwo.section.firstproject E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/ocr.jpg: open failed: EACCES (Permission denied)
05-19 18:02:24.936 21214-21214/com.fortytwo.section.firstproject E/JHEAD: can't open '/storage/emulated/0/Pictures/ocr.jpg'

Android Manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


<uses-sdk

    android:targetSdkVersion="23" />

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

主要活动

package com.fortytwo.section.firstproject;



public class MainActivity extends Activity {
public static final String PACKAGE_NAME = "com.fortytwo.section.firstproject";
public static final String DATA_PATH = Environment
        .getExternalStorageDirectory().toString() + "/SimpleAndroidOCR/";

// You should have the trained data file in assets folder
// You can get them at:
// http://code.google.com/p/tesseract-ocr/downloads/list
public static final String lang = "eng";

private static final String TAG = "MainActvity.java";

Button _button;
// protected ImageView _image;
protected EditText _field;
protected String _path;
protected boolean _taken;

protected static final String PHOTO_TAKEN = "photo_taken";

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    _field = (EditText) findViewById(R.id.field);
    _button = (Button) findViewById(R.id.btnCamera);
    _button.setOnClickListener(new ButtonClickHandler());

    _path = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)) + "/ocr.jpg";

    String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };



    // lang.traineddata file with the app (in assets folder)
    // You can get them at:
    // http://code.google.com/p/tesseract-ocr/downloads/list
    // This area needs work and optimization
    if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
        try {

            AssetManager assetManager = getAssets();
            InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
            GZIPInputStream gin = new GZIPInputStream(in);
            OutputStream out = new FileOutputStream(DATA_PATH
                    + "tessdata/" + lang + ".traineddata");

            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            //while ((lenf = gin.read(buff)) > 0) {
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            //gin.close();
            out.close();

            Log.v(TAG, "Copied " + lang + " traineddata");
        } catch (IOException e) {
            Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
        }
    }



    // _image = (ImageView) findViewById(R.id.image);

}

public class ButtonClickHandler implements View.OnClickListener {
    public void onClick(View view) {
        Log.v(TAG, "Starting Camera app");
        startCameraActivity();
    }
}

// Simple android photo capture:
// http://labs.makemachine.net/2010/03/simple-android-photo-capture/

protected void startCameraActivity() {
    File file = new File(_path);
    Uri outputFileUri = Uri.fromFile(file);

    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    Log.i(TAG, "resultCode: " + resultCode);

    if (resultCode == -1) {
        onPhotoTaken();
    } else {
        Log.v(TAG, "User cancelled");
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(MainActivity.PHOTO_TAKEN, _taken);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    Log.i(TAG, "onRestoreInstanceState()");
    if (savedInstanceState.getBoolean(MainActivity.PHOTO_TAKEN)) {
        onPhotoTaken();
    }
}

protected void onPhotoTaken() {
    _taken = true;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;

    Bitmap bitmap = BitmapFactory.decodeFile(_path, options);

    try {
        ExifInterface exif = new ExifInterface(_path);
        int exifOrientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        Log.v(TAG, "Orient: " + exifOrientation);

        int rotate = 0;

        switch (exifOrientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;

        }

        Log.v(TAG, "Rotation: " + rotate);

        if (rotate != 0) {

            // Getting width & height of the given image.
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            // Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
        }

        // Convert to ARGB_8888, required by tess
        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

    } catch (IOException e) {
        Log.e(TAG, "Couldn't correct orientation: " + e.toString());
    }

    // _image.setImageBitmap( bitmap );

    Log.v(TAG, "Before baseApi");

    TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.setDebug(true);
    baseApi.init(DATA_PATH, lang);
    baseApi.setImage(bitmap);

    String recognizedText = baseApi.getUTF8Text();

    baseApi.end();

    // You now have the text in recognizedText var, you can do anything with it.
    // We will display a stripped out trimmed alpha-numeric version of it (if lang is eng)
    // so that garbage doesn't make it to the display.

    Log.v(TAG, "OCRED TEXT: " + recognizedText);

    if ( lang.equalsIgnoreCase("eng") ) {
        recognizedText = recognizedText.replaceAll("[^a-zA-Z0-9]+", " ");
    }

    recognizedText = recognizedText.trim();

    if ( recognizedText.length() != 0 ) {
        _field.setText(_field.getText().toString().length() == 0 ? recognizedText : _field.getText() + " " + recognizedText);
        _field.setSelection(_field.getText().toString().length());
    }

    // Cycle done.
}

// www.Gaut.am was here
// Thanks for reading!
}

由于某种原因,错误被抛出。我已经要求在清单中获得许可,但它仍然被抛出。我有另一款具有相同功能的应用程序,可以完美运行。

0 个答案:

没有答案