简体   繁体   中英

Error When Save Bitmap To File on sdCard android

I have code to save Bitamp to file on sdcard but when i run this code it`s get this error

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

Code:

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

i try to add this on AndroidManifest.xml and doesn't work

=======

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>

I solve my problem when i change

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

To

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM