I have bitmap object and I want to convert it into File object , for that I have this code: (image is Bitmap object)
FileOutputStream out = null;
try {
out = new FileOutputStream(Environment.getDataDirectory().toString()+"/"+"test.png");
image.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
Log.d ("info", “ERROR 1");
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
Log.d ("info", “ERROR 2");
e.printStackTrace();
}
}
my manifest file is this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alessio.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
but I get this error:
10:57:27.573 29888-29888/com.example.alessio.myapplication W/System.err: java.io.FileNotFoundException: /data/test.png: open failed: EACCES (Permission denied)
08-20 10:57:27.573 29888-29888/com.example.alessio.myapplication W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
08-20 10:57:27.573 29888-29888/com.example.alessio.myapplication W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
08-20 10:57:27.573 29888-29888/com.example.alessio.myapplication W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
08-20 10:57:27.573 29888-29888/com.example.alessio.myapplication W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
I want to save file object in internal storage and I don't understand because I get this error.
Can you help me?
Do not use Environment.getDataDirectory()
.
If by "internal storage", you mean what the Android SDK refers to as internal storage , use getFilesDir()
, called on a Context
, such as your Activity
.
If by "internal storage", you mean what the user calls "internal storage" but the Android SDK refers to as external storage , use getExternalFilesDir()
(also on Context
) or Environment.getExternalStoragePublicDirectory()
.
In addition what CommonsWare said, don't use +"/"+ as path separator. use Environment.getPathSeparator()
Also take care you have a file writer that can create a file if not existent.
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.