Vlad Kramarenko Vlad Kramarenko - 8 days ago 9
Android Question

Bitmap to file android 6.0

I get image from url and save it in memory of device. I had this:

new AsyncTask<String, Void, Bitmap>() {
protected Bitmap doInBackground(String... iUrl) {
Bitmap bitmap = null;
try {
URL url = new URL("http://lookw.ru/1/73/1380310809-pitstsa-ch1--16.jpg");
bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "scetchpad";
File dir = new File(filepath);
File file = new File(dir, "pizza.png");
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
Log.d("LOGI", filepath);


} catch (Exception e) {

e.printStackTrace();
}
return bitmap;

}

@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
assert imageView != null;
imageView.setImageBitmap(bitmap);
}
}.execute();


permission

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


This code work on my tablet(android 4.1), but when I test it on my mobile phone(Xiaomi Mi Max android 6.0) i have some problem.

11-25 18:39:56.260 19383-19480/com.vkramarenko.myapplication W/System.err: java.io.FileNotFoundException: /storage/emulated/0/scetchpad/pizza.png: open failed: ENOENT (No such file or directory)
11-25 18:39:56.260 19383-19480/com.vkramarenko.myapplication W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
11-25 18:39:56.260 19383-19480/com.vkramarenko.myapplication W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
11-25 18:39:56.260 19383-19480/com.vkramarenko.myapplication W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
11-25 18:39:56.260 19383-19480/com.vkramarenko.myapplication W/System.err: at com.vkramarenko.myapplication.MainActivity$1.doInBackground(MainActivity.java:47)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: at com.vkramarenko.myapplication.MainActivity$1.doInBackground(MainActivity.java:38)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: at java.lang.Thread.run(Thread.java:818)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
11-25 18:39:56.261 19383-19480/com.vkramarenko.myapplication W/System.err: at libcore.io.Posix.open(Native Method)
11-25 18:39:56.262 19383-19480/com.vkramarenko.myapplication W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
11-25 18:39:56.262 19383-19480/com.vkramarenko.myapplication W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
11-25 18:39:56.262 19383-19480/com.vkramarenko.myapplication W/System.err: ... 10 more


And I also don't see this directory(/storage/emulated/0) in memory of device, but some file(icon of app) save on this directory.

Answer

You should ask for WRITE_EXTERNAL_STORAGE permission at rune time on Android 6.0 devices. Check here for more info : https://developer.android.com/training/permissions/requesting.html

Comments