English 中文(简体)
开放式的例外:欧安会(被拒签)
原标题:Exception open failed: EACCES (Permission denied) on Android

我正在 。

公开失败:EACCES(被拒)

页: 1 产出Stream myOutput = 新的文件OutputStream(outFileName);

我检查了根基,我尝试了<代码>android.permission。 EPAITE_EXTERNAL_STORAGE。

我如何解决这一问题?

try {
    InputStream myInput;

    myInput = getAssets().open("XXX.db");

    // Path to the just created empty db
    String outFileName = "/data/data/XX/databases/"
            + "XXX.db";

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    buffer = null;
    outFileName = null;
}
catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
最佳回答

我有同样的问题...... The <uses-permission was in the wrong place. 这是正确的:

 <manifest>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        ...
        <application>
            ...
            <activity> 
                ...
            </activity>
        </application>
    </manifest> 

The uses-permission tag needs to be outside the application tag.

问题回答

谷歌在Android Q上有一个新特征:外部储存的过滤观点。 www.un.org/Depts/DGACM/index_french.htm

<manifest ... >
    <!-- This attribute is "false" by default on apps targeting Android Q. -->
    <application android:requestLegacyExternalStorage="true" ... >
     ...
    </application>
</manifest>

https://developer.android.com

<><>Edit>: 我开始退席,因为这一答案已经过时。 因此,凡看到答案的人,请看上述联系,并读到指示。

执行部分第23段+,你需要要求获得读/正文许可,即使这些许可已经列在你的名单上。

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don t have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

AndroidManifest.xml

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

关于请求核准APIC23+的正式文件,支票https://developer.android.com/training/permissions/requesting.html

Add android:requestLegacyExternalStorage="true" to the Android Manifest It s worked with Android 10 (Q) at SDK 29+
or After migrating Android X.

 <application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon=""
    android:label=""
    android:largeHeap="true"
    android:supportsRtl=""
    android:theme=""
    android:requestLegacyExternalStorage="true">

我在主人提出申请时曾经看到过这一点。 在代用品环境中,你需要适当说明外部储存的规模(“SD卡”)。 缺席,“外部储存”现场是空洞的,这很可能意味着不存在这种装置,即使明示准许,也投下了ESCES。

除所有答复外,确保你不再使用你的电话作为USB的储存。

在允许的U 储存模式下,我对HTC Sensation也存在同样的问题。 我仍然可以 de笑/run,但我可以避免外部储存。

我的问题是“TargetApi(23)”问题,如果你的敏SdkVersion is bellow 23.的话,就需要这样做。

So, I have request permission with the following snippet

protected boolean shouldAskPermissions() {
    return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

@TargetApi(23)
protected void askPermissions() {
    String[] permissions = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"
    };
    int requestCode = 200;
    requestPermissions(permissions, requestCode);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
// ...
    if (shouldAskPermissions()) {
        askPermissions();
    }
}

了解解决办法:

<application ...
    android:requestLegacyExternalStorage="true" ... >

Is temporary, sooner or later your app should be migrated to use Scoped Storage.

在安森,你可以使用所建议的解决办法绕过系统限制,但在<代码>Android 11 (R)中,必须使用范围很广的存储<>代码,如果你坚持使用旧逻辑,你的话可能会中断!

。 这一录像可能是一个很好的帮助。

Android 10(API 29)介绍了《范围储存。 改变你要求储存遗产的姿态不是长期的解决办法。

我在取代我先前的<编码>Environment.getExternalStorageDirectory((与APIC29做解释)时,将这一问题定在context.getExternalFilesDir(null)

请注意,context.getExternalFilesDir(打字),如果储存地点没有,可退约,以确保你在有外部许可的情况下重新检查。

阅读

我也经历同样的情况。 我发现的是,如果你去了。 申请主管机构――> 申请主管机构-> 申请-> 许可-> 允许储存 ,解决了这一问题。

事实证明,这是一站不住脚的错误,因为我的电话仍然与台式电脑连接,没有实现这一点。

So I had to turn off the USB connection and everything worked fine.

我在《三星银河说明》3中也有同样的问题,该说明是第CM 12.1号。 对我来说,问题是:

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

and had to use it to take and store user photos. When I tried to load those same photos in ImageLoader i got the (Permission denied) error. The solution was to explicitly add

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

由于上述许可,只限读第18版,并准许阅读。

In addition to all answers, if the clients are using Android 6.0, Android added new permission model for (Marshmallow).

<>光线> 如果你针对的是第22版或以下的版本,那么你的申请将要求安装时间的所有许可,就像在任何操作低于马什洛的非洲顾问办公室的装置上一样。 如果你试图从6.0至6.0的推介器上.,那么,你需要明确走进“环境”和“资源”;“YAPP->”;如果你给予许可,则允许并修改许可。

在我的<斯特隆>之前,我的问题得到解决。 我改变了这一点:

File myFile= new File(Environment.getExternalStorageDirectory() + "newFile");

为此:

File myFile= new File(Environment.getExternalStorageDirectory() + "/newFile");

UPDATE: as mentioned in the comments, the right way to do this is:

File myFile= new File(Environment.getExternalStorageDirectory(), "newFile");

在<代码>直线上储存一个在相关目录上外的文件restricted aboveAPI 29+。 a. 为了生成新档案或创建新档案,使用您的申请书目录,如:

因此,正确的做法是:

val file = File(appContext.applicationInfo.dataDir + File.separator + "anyRandomFileName/")

你可以把任何数据写进这一生成的档案中!

The above file is accessible and would not throw any exception because it resides in your own developed app s directory.

另一种选择是android:requestLegacyExternalStorage=“true”,载于mani.4/1999/ application tag,正如Uriel所建议的那样,但不是永久解决办法!

我期望在<条码>/数据下的所有东西都属于“内部储存”。 但是,你应当能够写到<代码>/sdcard。

Change a permission property in your /system/etc/permission/platform.xml
and group need to mentioned as like below.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
    <group android:gid="sdcard_rw" />
    <group android:gid="media_rw" />    
</uses-permission>

I had the same error when was trying to write an image in DCIM/camera folder on Galaxy S5 (android 6.0.1) and I figured out that only this folder is restricted. I simply could write into DCIM/any folder but not in camera. This should be brand based restriction/customization.

当申请属于系统应用时,可以进入SD卡。

或许答案是:

在APIC >=23个装置,如果安装了电器(电灯没有系统照),你应检查“Setting - 申请”的储存许可,每套设备都有许可证清单,请在上面检查。 审判

keep in mind that even if you set all the correct permissions in the manifest: The only place 3rd party apps are allowed to write on your external card are "their own directories" (i.e. /sdcard/Android/data/) trying to write to anywhere else: you will get exception: EACCES (Permission denied)

In my case I was using a file picker library which returned the path to external storage but it started from /root/. And even with the WRITE_EXTERNAL_STORAGE permission granted at runtime I still got error EACCES (Permission denied).
So use Environment.getExternalStorageDirectory() to get the correct path to external storage.

Example:
Cannot write: /root/storage/emulated/0/newfile.txt
Can write: /storage/emulated/0/newfile.txt

boolean externalStorageWritable = isExternalStorageWritable();
File file = new File(filePath);
boolean canWrite = file.canWrite();
boolean isFile = file.isFile();
long usableSpace = file.getUsableSpace();

Log.d(TAG, "externalStorageWritable: " + externalStorageWritable);
Log.d(TAG, "filePath: " + filePath);
Log.d(TAG, "canWrite: " + canWrite);
Log.d(TAG, "isFile: " + isFile);
Log.d(TAG, "usableSpace: " + usableSpace);

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

Output 1:

externalStorageWritable: true
filePath: /root/storage/emulated/0/newfile.txt
isFile: false
usableSpace: 0

产出2:

externalStorageWritable: true
filePath: /storage/emulated/0/newfile.txt
isFile: true
usableSpace: 1331007488

我正在我的印章C.C.(与Nexus 7号电.相环)下制造一个倍数,并确实存在这一问题。

It turned out that giving the folder rw (666) permission was not sufficient and it had to be rwx (777) then it all worked!

如果您通过这些代行指挥拥有扎根的装置,可以绕过6号令的储存许可执行:

root@msm8996:/ # getenforce
getenforce
Enforcing
root@msm8996:/ # setenforce 0
setenforce 0
root@msm8996:/ # getenforce
getenforce
Permissive

i faced the same error on xiaomi devices (android 10 ). The following code fixed my problem. Libraries: Dexter(https://github.com/Karumi/Dexter) and Image picker(https://github.com/Dhaval2404/ImagePicker)

Add manifest ( android:requestLegacyExternalStorage="true")

    public void showPickImageSheet(AddImageModel model) {
    BottomSheetHelper.showPickImageSheet(this, new BottomSheetHelper.PickImageDialogListener() {
        @Override
        public void onChooseFromGalleryClicked(Dialog dialog) {
            selectedImagePickerPosition = model.getPosition();
            Dexter.withContext(OrderReviewActivity.this)                   .withPermissions(Manifest.permission.READ_EXTERNAL_STORAGE)
                    .withListener(new MultiplePermissionsListener() {
                        @Override
                        public void onPermissionsChecked(MultiplePermissionsReport report) {
                            if (report.areAllPermissionsGranted()) {
                                ImagePicker.with(OrderReviewActivity.this)
                                        .galleryOnly()
                                        .compress(512)
                                        .maxResultSize(852,480)
                               .start();
                            }
                        }

                        @Override
                        public void onPermissionRationaleShouldBeShown(List<PermissionRequest> list, PermissionToken permissionToken) {
                            permissionToken.continuePermissionRequest();
                        }

                    }).check();

            dialog.dismiss();
        }

        @Override
        public void onTakePhotoClicked(Dialog dialog) {
            selectedImagePickerPosition = model.getPosition();
            ImagePicker.with(OrderReviewActivity.this)
                    .cameraOnly()
                    .compress(512)
                    .maxResultSize(852,480)
                    .start();

            dialog.dismiss();
        }

        @Override
        public void onCancelButtonClicked(Dialog dialog) {
            dialog.dismiss();
        }
    });
}

In my case the error was appearing on the line

      target.createNewFile();

由于我无法在纸上建立一个新的档案,因此我不得不使用文件File办法。

      documentFile.createFile(mime, target.getName());

For the above question the problem may be solved with this approach,

    fos=context.getContentResolver().openOutputStream(documentFile.getUri());

See this thread too, How to use the new SD card access API presented for Android 5.0 (Lollipop)?

页: 1 采用以下程序处理甲型六氯环己烷和甲型六氯环己烷的情况

  1. 在我的案例中,作为按范围分列的原始文件/文件/文件储存的预处理文件; 按照要求和编号,Image/Video......;

  2. 在从我的外部储存中抽取时,在藏书上填写了文件。

  3. 然后,在上载时(在我发送/上载的纽顿点上)将文件从切除到我范围有限的储存地,然后做我的上载处理。

采用这一解决办法是因为在游乐仓库中及时装满载荷,它为开曼-西里尔-斯蒂尔-斯蒂尔-斯蒂尔-斯蒂尔-斯蒂尔-斯蒂尔-斯蒂尔-斯蒂尔-斯蒂尔-斯里尔-斯里尔-萨里尔-萨里尔-斯里尔-萨里尔-萨里尔-许可带来了警告,有时在我的案件中,从游戏仓库中被拒。

而且,当我们使用目标第30号时,我们就能够分享或从我们的内部储存中转交档案,以便评估

2022 要求许可的基特林方式:

private val writeStoragePermissionResult =
   registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->}

private fun askForStoragePermission(): Boolean =
   if (hasPermissions(
           requireContext(),
           Manifest.permission.READ_EXTERNAL_STORAGE,
           Manifest.permission.WRITE_EXTERNAL_STORAGE
       )
   ) {
       true
   } else {
       writeStoragePermissionResult.launch(
           arrayOf(
               Manifest.permission.READ_EXTERNAL_STORAGE,
               Manifest.permission.WRITE_EXTERNAL_STORAGE,
           )
       )
       false
   }

fun hasPermissions(context: Context, vararg permissions: String): Boolean = permissions.all {
    ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签