【问题标题】:Error When Save Bitmap To File on sdCard android在 sdCard android 上将位图保存到文件时出错
【发布时间】:2013-06-18 17:55:04
【问题描述】:

我有代码将 Bitamp 保存到 sdcard 上的文件,但是当我运行此代码时,它会收到此错误

java.io.IOException: open failed: EACCES (Permission denied)

代码:

 Bitmap photo = extras.getParcelable("data");
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                    File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
                    try {
                            f.createNewFile();
                            FileOutputStream fo = new FileOutputStream(f);
                            fo.write(bytes.toByteArray());
                            fo.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.d("ERROR", e.toString());
                    }

我尝试在 AndroidManifest.xml 上添加它,但不起作用

=======

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera"></uses-feature> 

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

【问题讨论】:

  • 尝试使用两参数 File 构造函数,而不是字符串连接。另外,请考虑使用getExternalFilesDir()Environment.getExternalStoragePublicDirectory(),而不是将文件转储到外部存储的根目录中。
  • 同样的错误
  • 你是在模拟器上运行的吗?
  • 不,我在我的 Galaxy s3 上测试过
  • 请发布整个清单。

标签: android file bitmap sd-card


【解决方案1】:

当我改变时我解决了我的问题

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

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

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 2015-09-27
    • 1970-01-01
    • 2012-09-02
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多