使用Camera Android时EACCES(权限被拒绝)

时间:2015-07-07 15:11:34

标签: android

我想从Android相机中获取并存储照片。这是代码:

public void avviaFotocamera(View v){
        this.launchCamera();
    }

    private void launchCamera() {
        try {
            mOutputFile = new File(getExternalStorageDirectory(),  "temp.jpg");
            Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intentCamera.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(mOutputFile));
            startActivityForResult(intentCamera, CAMERA_REQUEST);
        }   catch (Exception e) {
            Toast t = Toast.makeText(this, "Si è verificato un errore durante l'acquisizione dell'immagine:\n" + e.toString(), Toast.LENGTH_LONG);
            t.show();
        }
    }

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

            if(requestCode == CAMERA_REQUEST) {
                try {
                    Bitmap datiFoto = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(),
                                      Uri.fromFile(mOutputFile));
                    saveBitmap(datiFoto);
                    mOutputFile.delete();
                }   catch (Exception e) {
                    Toast t = Toast.makeText(this, "Si è verificato un errore durante l'acquisizione dell'immagine:\n" + e.toString(), Toast.LENGTH_LONG);
                    t.show();
                }
            }
        }

    private void saveBitmap(Bitmap datiFoto) {
        try {
            //Nome del file da assegnare all'immagine
            final String fileName = "prova.jpg";
            FileOutputStream out = new FileOutputStream(getExternalStorageDirectory ()+fileName);
            datiFoto.compress(Bitmap.CompressFormat.JPEG, 90, out);
        }   catch (Exception e) {
            Toast t = Toast.makeText(this, "Si è verificato un errore durante il salvataggio dell'immagine:\n" + e.toString(), Toast.LENGTH_LONG);
            t.show();
            e.printStackTrace();
        }
    }

然而我收到此错误:java.io.FileNotFoundException:/prova.jpg:open failed:EACCES(Permission denied)。 这是我添加所有必需权限的清单:

 <uses-sdk
        android:maxSdkVersion="22"
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="22" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="22" />
    <uses-permission android:name="android.permission.SET_DEBUG_APP" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-feature android:name="android.hardware.camera"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.javapapers.android.maps.path.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  

我该如何解决?

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试将文件另存为/prova.jpg,该文件位于设备的根目录中。您的应用无法在那里存储文件。看来这条线没有按预期工作:

FileOutputStream out = new FileOutputStream(getExternalStorageDirectory ()+fileName);

也许试试:

File outFile = new File(Environment.getExternalStorageDirectory(), "prova.jpg");
FileOutputStream out = new FileOutputStream(outFile);