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

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