Reputation: 129
I'm trying to create a file on external storage but android is throwing error while doing that.
I have already gone through many questions asked on stackoverflow but still unable to find the problem.
I have provided permissions in manifest file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jewelmahal.jewellerapp">
<uses-feature android:name="android.hardware.camera" android:required="true" />
<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=".login.LoginActivity"
android:label="@string/title_activity_login"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".signup.CompanyDetailsSignupActivity"
android:label="@string/title_activity_signup"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".signup.UploadTinActivity"
android:label="@string/upload_tin_title"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
AsyncTask
private class SaveImageToSD extends AsyncTask<Bitmap, Integer, String> {
private Bitmap tinImageBitmap;
protected void onPreExecute() {
//Nothing yet
}
protected String doInBackground(Bitmap... bitmaps) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
tinImageBitmap = bitmaps[0];
tinImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
return null; //Cannot write to Strorage
}
Log.d("EXTERNAL_STORAGE", "AVAILABLE FOR READ/WRITE");
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "JewelMahal");
Log.d("Can Write", Environment.getExternalStorageDirectory().canWrite() + "");
if (!file.mkdirs() && !file.isDirectory()) {
Log.d("LOG_TAG", "Directory not created");
}
else {
Log.d("LOG_TAG", "Directory created");
}
File destination = new File(file.getAbsolutePath() + File.separator + TIN_FILE_PREFIX + System.currentTimeMillis() + ".jpg");
Log.d("Location:", destination.getAbsolutePath());
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
Log.d("FileNotFound", "dsda");
e.printStackTrace();
} catch (IOException e) {
Log.d("IOException", "dsda");
e.printStackTrace();
}
return (tinImagePath=destination.getAbsolutePath());
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String tinPath) {
tinPreview.setImageBitmap(tinImageBitmap);
}
}
Error
02-10 09:14:52.206 29864-32486/? W/System.err: java.io.IOException: open failed: ENOENT (No such file or directory)
02-10 09:14:52.215 29864-32486/? W/System.err: at java.io.File.createNewFile(File.java:939)
02-10 09:14:52.216 29864-32486/? W/System.err: at com.jewelmahal.jewellerapp.signup.UploadTinActivity$SaveImageToSD.doInBackground(UploadTinActivity.java:65)
02-10 09:14:52.216 29864-32486/? W/System.err: at com.jewelmahal.jewellerapp.signup.UploadTinActivity$SaveImageToSD.doInBackground(UploadTinActivity.java:33)
02-10 09:14:52.216 29864-32486/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
02-10 09:14:52.216 29864-32486/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-10 09:14:52.216 29864-32486/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
02-10 09:14:52.216 29864-32486/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
02-10 09:14:52.216 29864-32486/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
02-10 09:14:52.216 29864-32486/? W/System.err: at java.lang.Thread.run(Thread.java:818)
02-10 09:14:52.217 29864-32486/? W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
02-10 09:14:52.220 29864-32486/? W/System.err: at libcore.io.Posix.open(Native Method)
02-10 09:14:52.220 29864-32486/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
02-10 09:14:52.220 29864-32486/? W/System.err: at java.io.File.createNewFile(File.java:932)
02-10 09:14:52.220 29864-32486/? W/System.err: ... 8 more
Upvotes: 3
Views: 472
Reputation: 172
You need to grant permissions explicitly for it to work on Marshmallow.
Either change the target to Lollipop in your build.gradle or request for permission.
You can also go to settings, apps. Your app, and grant permission there manually.
Here is a nice guide to request permission:
https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html
Upvotes: 2