在SD卡上保存图像时权限被拒绝

时间:2012-09-07 15:51:51

标签: android file permissions sd-card

此函数用于在SD卡上保存图像(我仅在模拟器上测试过它):

public String SaveImage(String URL,String imagename){

        Bitmap bitmap = null;
        InputStream in = null;
        String path = Environment.getExternalStorageDirectory().toString();
        File dir = new File(path + "/bh");
        if(!dir.exists())
             new File(path + "/bh").mkdir();
        Log.i("in save()", "after file");
        File mImageFile = new File(path+"/bh/"+imagename);

        if(mImageFile.exists())
            Toast.makeText(mContext, "Saved", Toast.LENGTH_LONG).show();

        try{
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            FileOutputStream out = new FileOutputStream(mImageFile);
            bitmap.compress(CompressFormat.JPEG, 100,out);
            out.flush();
            out.close();
            in.close();
            return imagename+".jpg";
        }catch(IOException ex){
            Log.e("==== Error in saving image ====",ex.getMessage());
            return "";
        }
    }

我添加了以下所需的权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="mobile.bh"
    android:versionCode="4"
    android:versionName="1.0.3" >

    <uses-sdk android:minSdkVersion="8" />

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

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="mobile.bh.activities.BHActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="mobile.bh.activities.RecipesListActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="mobile.bh.activities.RecipeInfoActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="mobile.bh.activities.IngredientsActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="mobile.bh.activities.MethodActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="mobile.bh.activities.SpicesListActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity android:name=".activities.SpicesCategoriesActivity" >
        </activity>
        <activity android:name=".activities.CategoryActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

您还需要使用Internet权限,因为您已连接到互联网。

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

答案 1 :(得分:0)

您的模拟器是否支持SDCard?

看看这个好的答案,可能是问题https://stackoverflow.com/a/11468278/1635817