位图保存到文件功能 [英] Save bitmap to file function

查看:471
本文介绍了位图保存到文件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图挽救一个位图文件和特定的直接使用我创建了一个功能。它不工作。 bitmap.com preSS部分(here3前)后,它崩溃。

 文件DIR =新的文件(文件路径);

    如果dir.mkdirs()(dir.exists()!);

    档案文件=新的文件(Environment.getExternalStorageDirectory()+文件路径,一边+png格式);
    FileOutputStream中FOUT =新的FileOutputStream(文件);

    bitmap.com preSS(Bitmap.Com pressFormat.PNG,85,FOUT);
    fOut.flush();
    fOut.close();

    的System.out.println(文件路径);

    bitmap.recycle();
    System.gc()的;
 


错误日志:

  06-29 00:16:38.089:D / AndroidRuntime(3260):关闭虚拟机
06-29 00:16:38.089:W / dalvikvm(3260):主题ID = 1:螺纹退出与未捕获的异常(组= 0xb587f4f0)
06-29 00:16:38.089:E / AndroidRuntime(3260):致命异常:主要
06-29 00:16:38.089:E / AndroidRuntime(3260):java.lang.RuntimeException的:不提供结果ResultInfo {谁= NULL,请求= 0,结果= -1,数据= {意图行事= android.intent。 action.VIEW dat=content://org.openintents.filemanager/mimetype//mnt/sdcard/download/02977_awreckedboatintheocean_1280x1024.jpg }}到活动{com.polygonattraction.testbirds / com.polygonattraction.testbirds.functions.SelectImageSource}:java.lang.IllegalStateException:不能COM preSS再循环的位图
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.app.ActivityThread.access $ 2000(ActivityThread.java:117)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:961)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.os.Handler.dispatchMessage(Handler.java:99)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.os.Looper.loop(Looper.java:130)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.app.ActivityThread.main(ActivityThread.java:3683)
06-29 00:16:38.089:E / AndroidRuntime(3260):在java.lang.reflect.Method.invokeNative(本机方法)
06-29 00:16:38.089:E / AndroidRuntime(3260):在java.lang.reflect.Method.invoke(Method.java:507)
06-29 00:16:38.089:E / AndroidRuntime(3260):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
06-29 00:16:38.089:E / AndroidRuntime(3260):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-29 00:16:38.089:E / AndroidRuntime(3260):在dalvik.system.NativeStart.main(本机方法)
06-29 00:16:38.089:E / AndroidRuntime(3260):java.lang.IllegalStateException:产生的原因无法COM preSS再循环的位图
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.graphics.Bitmap.checkRecycled(Bitmap.java:180)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.graphics.Bitmap.com preSS(Bitmap.java:581)
06-29 00:16:38.089:E / AndroidRuntime(3260):在com.polygonattraction.testbirds.functions.Functions.SaveToFile(Functions.java:144)
06-29 00:16:38.089:E / AndroidRuntime(3260):在com.polygonattraction.testbirds.functions.SelectImageSource.onActivityResult(SelectImageSource.java:113)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-29 00:16:38.089:E / AndroidRuntime(3260):在android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
 

解决方案

1 - 你需要在amnifest适当的权限:

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

2-了out.flush()检查出不为空。

3 -

 字符串FILE_PATH = Environment.getExternalStorageDirectory()。getAbsolutePath()+
                            / PhysicsSketchpad;
    文件DIR =新的文件(FILE_PATH);
如果(!dir.exists())
    dir.mkdirs();
    档案文件=新的文件(目录,画板+ pad.t_id +巴纽);
    FileOutputStream中FOUT =新的FileOutputStream(文件);

    bmp.com preSS(Bitmap.Com pressFormat.PNG,85,FOUT);
    fOut.flush();
    fOut.close();
 

I'm trying to save a bitmap to file and a specific directly using a function I've created. It's not working. It crashes after bitmap.compress part (before here3).

    File dir = new File(filepath);

    if(!dir.exists())dir.mkdirs();

    File file = new File(Environment.getExternalStorageDirectory() + filepath, side + ".png");
    FileOutputStream fOut = new FileOutputStream(file);

    bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
    fOut.flush();
    fOut.close();

    System.out.println(filepath);

    bitmap.recycle();
    System.gc();


Error log:

06-29 00:16:38.089: D/AndroidRuntime(3260): Shutting down VM
06-29 00:16:38.089: W/dalvikvm(3260): threadid=1: thread exiting with uncaught exception (group=0xb587f4f0)
06-29 00:16:38.089: E/AndroidRuntime(3260): FATAL EXCEPTION: main
06-29 00:16:38.089: E/AndroidRuntime(3260): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=android.intent.action.VIEW dat=content://org.openintents.filemanager/mimetype//mnt/sdcard/download/02977_awreckedboatintheocean_1280x1024.jpg }} to activity {com.polygonattraction.testbirds/com.polygonattraction.testbirds.functions.SelectImageSource}: java.lang.IllegalStateException: Can't compress a recycled bitmap
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.os.Looper.loop(Looper.java:130)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at java.lang.reflect.Method.invoke(Method.java:507)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at dalvik.system.NativeStart.main(Native Method)
06-29 00:16:38.089: E/AndroidRuntime(3260): Caused by: java.lang.IllegalStateException: Can't compress a recycled bitmap
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.graphics.Bitmap.checkRecycled(Bitmap.java:180)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.graphics.Bitmap.compress(Bitmap.java:581)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at com.polygonattraction.testbirds.functions.Functions.SaveToFile(Functions.java:144)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at com.polygonattraction.testbirds.functions.SelectImageSource.onActivityResult(SelectImageSource.java:113)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-29 00:16:38.089: E/AndroidRuntime(3260):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)

解决方案

1 - you need an appropriate permission in amnifest:

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

2- out.flush() check the out is not null..

3 -

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                            "/PhysicsSketchpad";
    File dir = new File(file_path);
if(!dir.exists())
    dir.mkdirs();
    File file = new File(dir, "sketchpad" + pad.t_id + ".png");
    FileOutputStream fOut = new FileOutputStream(file);

    bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
    fOut.flush();
    fOut.close();

这篇关于位图保存到文件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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