0

此功能用于将图像保存在 sdcard 上(我仅在模拟器上测试过):

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>
4

2 回答 2

0

由于您要连接到 Internet,因此您还需要使用 Internet 权限。

<uses-permission android:name="android.permission.INTERNET" />
于 2012-09-07T16:04:22.383 回答
0

你的模拟器支持 SDCard 吗?

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

于 2012-09-07T16:41:08.780 回答