Android - How to get around the "Missing space on device" error

So, my last application encounters this problem when it complains about the logarithm that there is no place on the device to save files, but this is definitely not the case, since I can close my application by opening the camera for storage and take a picture. How everyone else solved this problem.

Edit: An error occurs in this method

private void writeFile(Bitmap bmp, File f) { FileOutputStream out = null; try { out = new FileOutputStream(f); bmp.compress(Bitmap.CompressFormat.PNG, 80, out);//<---error here } catch (NullPointerException e) { e.printStackTrace(); Log.w("nullpointerException on image error", "nullpointer"); } catch (FileNotFoundException e) { Log.w("fileNotfoundException on image error", "filenotfound"); // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (out != null ) out.close(); } catch(Exception ex) {} } } 

and this is the logcat message about this error:

 java.io.IOException: no space left on device at org.apache.harmony.luni.platform.OSFileSystem.write(Native Method) at dalvik.system.BlockGuard$WrappedFileSystem.write(BlockGuard.java:171) at java.io.FileOutputStream.write(FileOutputStream.java:300) at android.graphics.Bitmap.nativeCompress(Native Method) at data.ImageManager.writeFile(ImageManager.java:215) at data.ImageManager.getBitmap(ImageManager.java:192) at data.ImageManager.access$1(ImageManger.java:102) at data.ImageManager$ImageQueueManager.run(ImageManager.java:290) at java.lang.Thread.run(Thread.java:1019) 

Edit: Heres like me, although you create a directory in external memory

This is what I use to create a place that I thought would be an SD card

  String sdState = android.os.Environment.getExternalStorageState(); if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) { File sdDir = android.os.Environment.getExternalStorageDirectory(); cacheDir = new File(sdDir,"data/gr"); } else cacheDir = context.getExternalCacheDir(); if(!cacheDir.exists()) cacheDir.mkdirs(); if(sdState.equals(android.os.Environment.MEDIA_MOUNTED)){ File adSdDir = android.os.Environment.getExternalStorageDirectory(); adCacheDir = new File(adSdDir,"data/ad"); }else adCacheDir = context.getExternalCacheDir(); if(!adCacheDir.exists()) adCacheDir.mkdirs(); } 

and then in the method I'm looking for or creating an image to go into the directory:

  private Bitmap getBitmap(ImgRequestObj ids) { String url = null; String filename = ids.objId.toString(); File f = null; try{ if(ids.objName.equals("mark")){ url = graffMarksURL; f = new File(cacheDir, filename); }else if(ids.objName.equals("admark")){ url = adMarksURL; f = new File(adCacheDir, filename); } // Is the bitmap in our cache? Bitmap bitmap = BitmapFactory.decodeFile(f.getPath()); if(bitmap != null) return bitmap; // Nope, have to download it try { BitmapFactory.Options bfOptions=new BitmapFactory.Options(); bfOptions.inDither=false; //Disable Dithering mode bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future DefaultHttpClient client; HttpResponse response = null; HttpParams params = null;// new Http parameters to be used for extra buffering room in VM HttpPost post = null; InputStream is = null; BufferedInputStream bis = null; Bitmap bmp = null; ArrayList<NameValuePair> nvp1 = new ArrayList<NameValuePair>(); nvp1.add(new BasicNameValuePair("id", ids.objId.toString())); // - martin passing id to PHP  select statement client = new DefaultHttpClient(); try { post = new HttpPost(url); params = post.getParams();//Setting the new Http params to post post.setEntity(new UrlEncodedFormEntity(nvp1)); try{ HttpConnectionParams.setSocketBufferSize(params, 8192);//Defining the new buffer size response = client.execute(post); } catch(HttpResponseException e){ e.getCause(); } //Capture response from query and prepare as input HttpEntity entity = response.getEntity(); // move content to Input Stream is = entity.getContent(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } bis = new BufferedInputStream(is); try{ bmp = BitmapFactory.decodeStream(bis, null, bfOptions); }finally{} if( is != null ) try { is.close(); //response = null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Log.w("inputstream of image closed", "for this image "+id.toString()); if( bis != null ) try { //response = null; bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } bitmap = bmp; writeFile(bitmap, f); try{ bmp = null; }catch(NullPointerException e){ e.printStackTrace(); } return bitmap;} catch(Exception e){ e.printStackTrace(); } return bitmap; }catch(NullPointerException e){ e.printStackTrace(); } return null; } 
+1
source share
2 answers

you are actually using internal memory using getCacheDir() in your else statement. However, if you want to store big data, it is recommended to use getExternalCacheDir() , because not all Android phones have huge internal storage, but they have / may have more external storage.

 if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) { File sdDir = context.getExternalCacheDir(); cacheDir = new File(sdDir,"data"); } else Log.e("storage", "SD card not found"); 
+1
source

As a result, I changed my code, where I create my directories:

  String sdState = android.os.Environment.getExternalStorageState(); if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) { File sdDir = android.os.Environment.getExternalStorageDirectory(); cacheDir = new File(sdDir,"data/gr"); } else cacheDir = context.getExternalFilesDir(sdState); <--changed if(!cacheDir.exists()) cacheDir.mkdirs(); if(sdState.equals(android.os.Environment.MEDIA_MOUNTED)){ File adSdDir = android.os.Environment.getExternalStorageDirectory(); adCacheDir = new File(adSdDir,"data/ad"); }else adCacheDir = context.getExternalFilesDir(sdState); <--changed if(!adCacheDir.exists()) adCacheDir.mkdirs(); } 

Until now, using this, I can save images that I shoot with a camera with all the images in my upload area again.

0
source

Source: https://habr.com/ru/post/915011/


All Articles