Tôi đang cố gắng lưu một bitmap vào tệp và một tệp cụ thể trực tiếp bằng cách sử dụng một hàm tôi đã tạo. Nó không hoạt động. Nó treo sau khi bitmap.compress một phần (trước đây ở đây3).Lưu bitmap vào chức năng tập tin
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();
Lỗi đăng nhập:
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)
whats đầu ra logcat của bạn? –
"Không thể nén bitmap tái chế" .bạn đã gọi bitmap.recycle(); bất kỳ nơi nào trước đây? –
Tôi đã gọi nó chỉ sau khi nó giả sử đã lưu nó, tôi đã cố gắng loại bỏ điều đó nhưng tôi vẫn nhận được cùng một lỗi. –