Android 关于内存权限的问题,坑呀!!!!!!!!!顺便记录一些leancloud的东西

用leancloud作为数据库,在储存图片的时候产生了一些问题

首先是leancloud可以用Pointer来存储对象,但是这个被存储的对象一定要先存好,不然会有问题

        final AVObject object = new AVObject("Login");
        object.put("id",id);
        object.put("psw",psw);
        final AVObject info = new AVObject("Info");
        info.put("name",name);
        info.put("id",id);
        info.saveInBackground();            //这个要是没有,下面就会报错
        object.put("info",info);
        object.saveInBackground();
        

 

然后是leancloud的储存不能在主线程中调用,所以要么用saveInBackground,要么自己开一个线程

 

关于储存文件,AVFile

一上来先犯了个蠢错误,AVFile.withAbsoluteLocalPath这个路径很明显是模拟器上的路径,结果我用电脑上的路径一顿试,哎呀真的傻。

然后就是一直FileNotFoundException,当时以为这个是文件不存在,然后改了下文件路径的写法

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/cat1.png"

这样写确实好,但是问题不在这里

把错误整个打到日志里,发现是权限问题:

com.avos.avoscloud.AVException: com.avos.avoscloud.AVException: java.io.FileNotFoundException: /storage/emulated/0/Download/cat1.png: open failed: EACCES (Permission denied)

说句实话早该想到了,现在可能还没有这个思维,以前写东西从来没有权限这类东西,突然一下有点转不过来

加上权限:

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

还是不对,继续找,发现这个权限是protected的,需要手动申请,书上以前讲过,我也试过,结果忘求了,哎

// 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
        );
    }
}

(来自StackOverflow:https://stackoverflow.com/questions/23527767/open-failed-eacces-permission-denied

然后还是不对?????????????????????????

继续看,发现还有一个要加的:

        android:requestLegacyExternalStorage="true"

同样来自上面的网站

问题解决,我也疯了,下午就搞了个这个,fuck

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章