Android的 - 如何解决A"左设备上与QUOT没有空间;错误 [英] Android - How to work around a "no space left on device" error

查看:150
本文介绍了Android的 - 如何解决A"左设备上与QUOT没有空间;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的最新的应用程序运行到这个问题,即它抱怨在那里有没有剩余空间在设备上保存文件的logcat但绝对不是这样的,因为我可以关闭我的应用程序打开股票相机和拍照。如何有其他人处理这个问题。

编辑:在这个方法中出现的错误

 私人无效WriteFile的(BMP位图,文件f){
    FileOutputStream中走出= NULL;    尝试{
        OUT =新的FileOutputStream(F);
        bmp.com preSS(Bitmap.Com pressFormat.PNG,80出); //< ---这里的错误
    }赶上(NullPointerException异常五){
        e.printStackTrace();
        Log.w(图像错误NullPointerException异常,空指针);
    }赶上(FileNotFoundException异常五){
        Log.w(filenotfound,图像错误FileNotFoundException异常);
        // TODO自动生成catch块
        e.printStackTrace();
    }
    最后{
        尝试{如果out.close()(满分!= NULL); }
        赶上(例外前){}
    }
}

这是这个错误的logcat的报告:

  java.io.IOException异常:没有剩余空间装置
在org.apache.harmony.luni.platform.OSFileSystem.write(本机方法)
在dalvik.system.BlockGuard $ WrappedFileSystem.write(BlockGuard.java:171)
在java.io.FileOutputStream.write(FileOutputStream.java:300)
在android.graphics.Bitmap.nativeCom preSS(本机方法)
在data.ImageManager.writeFile(ImageManager.java:215)
在data.ImageManager.getBitmap(ImageManager.java:192)
在data.ImageManager.access $ 1(ImageManger.java:102)
在data.ImageManager $ ImageQueueManager.run(ImageManager.java:290)
在java.lang.Thread.run(Thread.java:1019)

编辑:继承人如何我虽然创建外部存储器上的目录

这是即时通讯使用创造,我认为将是SD卡的地方是什么

 字符串sdState = android.os.Environment.getExternalStorageState();
    如果(sdState.equals(android.os.Environment.MEDIA_MOUNTED)){
        文件sdDir = android.os.Environment.getExternalStorageDirectory();
        cacheDir =新的文件(sdDir,数据/克);
    }
    其他
        cacheDir = context.getExternalCacheDir();    如果(!cacheDir.exists())
        cacheDir.mkdirs();
    如果(sdState.equals(android.os.Environment.MEDIA_MOUNTED)){
        文件adSdDir = android.os.Environment.getExternalStorageDirectory();
        adCacheDir =新的文件(adSdDir,数据/信息);
    }其他
        adCacheDir = context.getExternalCacheDir();    如果(!adCacheDir.exists())
        adCacheDir.mkdirs();
}

,然后在我寻找或创造的形象进入该目录的方法:

 私人位图getBitmap(ImgRequestObj IDS){
    字符串URL = NULL;
    字符串文件名= ids.objId.toString();
    文件F = NULL;    尝试{
        如果(ids.objName.equals(标记)){
            URL = graffMarksURL;
            F =新的文件(cacheDir,文件名);
        }否则如果(ids.objName.equals(admark)){
            URL = adMarksURL;
            F =新的文件(adCacheDir,文件名);
        }    //是我们高速缓存中的位图?
    位图位图= BitmapFactory.de codeFILE(f.getPath());
    如果(!=位图NULL)返回位图;    //不,必须下载
    尝试{
            BitmapFactory.Options bfOptions =新BitmapFactory.Options();            bfOptions.inDither = FALSE; //禁用抖动模式            bfOptions.inPurgeable = TRUE; //告诉GC,它是否需要释放内存,位图可被清除            bfOptions.inInputShareable = TRUE; //哪一种参考将用于被明确后恢复位图数据,什么时候会在将来被使用
            DefaultHttpClient客户端;
            HTT presponse响应= NULL;
            的HttpParams PARAMS =无效;用于在虚拟机额外的缓冲室//新HTTP参数
            HttpPost后= NULL;
            InputStream为= NULL;
            二BufferedInputStream为= NULL;
            BMP位图= NULL;
            ArrayList的<&的NameValuePair GT; nvp1 =新的ArrayList<&的NameValuePair GT;();            nvp1.add(新BasicNameValuePair(ID,ids.objId.toString()));
            // - 马丁传球的ID php脚本select语句
             客户端=新DefaultHttpClient();            尝试{                后=新HttpPost(URL);
                PARAMS = post.getParams(); //设置新的HTTP PARAMS发布
                post.setEntity(新UrlEn codedFormEntity(nvp1));
                尝试{
                HttpConnectionParams.setSocketBufferSize(PARAMS,8192); //定义新的缓冲区大小
                响应= client.execute(岗位);                }
                抓(HTT presponseException E){
                    e.getCause();
                                    }
            从查询和prepare作为输入//捕获响应
            HttpEntity实体= response.getEntity();
            //移动内容输入流
                是= entity.getContent();            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }                双=新的BufferedInputStream(是);
                尝试{
                BMP = BitmapFactory.de codeStream(之二,空,bfOptions);
                }最后{}                    如果(是!= NULL)
                        尝试{
                            is.close();
                            //响应=无效;
                        }赶上(IOException异常五){
                            // TODO自动生成catch块
                            e.printStackTrace();
                        }
                    图像//Log.w(\"inputstream闭,这个形象+ id.toString());
                    如果(之二!= NULL)
                        尝试{
                            //响应=无效;
                            bis.close();
                        }赶上(IOException异常五){
                            // TODO自动生成catch块
                            e.printStackTrace();
                        }
                    位图= BMP;
                    写文件(位图,F);
                    尝试{
                        BMP = NULL;
                    }赶上(NullPointerException异常五){
                        e.printStackTrace();
                    }
                    返回位图;}
                赶上(例外五){
            e.printStackTrace();
            }
        返回位图;
    }赶上(NullPointerException异常五){
        e.printStackTrace();
    }
    返回null;
        }


解决方案

你实际上是使用内部存储器使用 getCacheDir()在else语句。但是,如果要存储大量数据,那么它的推荐使用 getExternalCacheDir(),因为并不是所有的Andr​​oid手机拥有庞大的内部存储空间,但他们确实/可能会有较大的外部存储。

 如果(sdState.equals(android.os.Environment.MEDIA_MOUNTED)){
   文件sdDir = context.getExternalCacheDir();
   cacheDir =新的文件(sdDir,数据);
}
其他
   Log.e(存储,SD卡未找到);

So my latest app runs into this problem where it complains in the logcat that theres no room left on the device to save files but that is definitely not the case as I can close my app open the stock camera and take a picture. How has everybody else dealt with this problem.

Edit: The 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 report on 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 how I though you create a directory on external memory

This is what Im using to create the place that I thought would be the 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 that I look for or create the 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 script 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;
        }

解决方案

you are actually using internal memory by using getCacheDir() in your else statement. However, if you want to store large data then its recommended to use getExternalCacheDir() because not all android phones have huge internal storage, but they do/may have bigger 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");

这篇关于Android的 - 如何解决A&QUOT;左设备上与QUOT没有空间;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆