我只能保存1張圖像,此代碼保存了最後一次拍攝的圖像,因此舊圖像會自動刪除。我應該保存大量的圖像到SD卡,我該怎麼辦才能修復此代碼?在Android中將圖像保存到SD卡
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_ACTION_CODE) {
// 2
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imagev.setImageBitmap(thumbnail);
// 3
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// 4
String filename;
Date date = new Date(0);
SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss");
filename = sdf.format(date);
File file = new
File(Environment.getExternalStorageDirectory()+File.separator+filename+".jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
// 5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我想這代碼,但仍然採取錯誤的logcat:
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); filename = sdf.format(new Date());
我意識到這個問題是不是日期格式。
仍以錯誤的logcat例如:
12-20 08:48:32.120: E/SoundPool(288): error loading /system/media/audio/ui/Lock.ogg
12-20 08:48:32.120: E/SoundPool(288): error loading /system/media/audio/ui/Unlock.ogg
12-20 08:48:32.620: E/EventHub(288): could not get driver version for /dev/input/mouse0, Not a typewriter
12-20 08:48:32.620: E/EventHub(288): could not get driver version for /dev/input/mice, Not a typewriter
12-20 08:48:32.720: E/Trace(288): error opening trace file: No such file or directory (2)
12-20 08:48:33.800: E/CommandListener(34): Failed to open /proc/sys/net/ipv6/conf/wlan0/disable_ipv6: No such file or directory
12-20 08:48:33.840: E/WifiStateMachine(288): Failed to disable IPv6: java.lang.IllegalStateException: command '1 interface ipv6 wlan0 disable' failed with '400 1 Failed to change IPv6 state (No such file or directory)'
12-20 08:48:33.920: E/MobileDataStateTracker(288): default: Ignoring feature request because could not acquire PhoneService
12-20 08:48:33.920: E/MobileDataStateTracker(288): default: Could not enable APN type "default"
12-20 08:48:36.850: E/Trace(349): error opening trace file: No such file or directory (2)
12-20 08:48:38.330: D/dalvikvm(288): GC_CONCURRENT freed 273K, 9% free 4588K/5012K, paused 8ms+31ms, total 516ms
12-20 08:48:38.330: D/dalvikvm(288): WAIT_FOR_CONCURRENT_GC blocked 284ms
12-20 08:48:38.340: D/dalvikvm(288): WAIT_FOR_CONCURRENT_GC blocked 335ms
12-20 08:48:38.350: D/dalvikvm(288): WAIT_FOR_CONCURRENT_GC blocked 317ms
12-20 08:48:38.350: I/ActivityManager(288): Start proc com.android.inputmethod.latin for service com.android.inputmethod.latin/.LatinIME: pid=377 uid=10018 gids={50018}
12-20 08:48:38.360: D/dalvikvm(288): WAIT_FOR_CONCURRENT_GC blocked 254ms
12-20 08:48:38.590: E/Trace(377): error opening trace file: No such file or directory (2)
12-20 08:48:39.500: E/ThrottleService(288): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
12-20 08:48:40.261: E/Trace(394): error opening trace file: No such file or directory (2)
12-20 08:48:42.171: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
12-20 08:48:42.331: E/ActivityThread(377): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
12-20 08:48:42.502: E/BinaryDictionaryGetter(377): Could not find a dictionary pack
12-20 08:48:43.362: I/ActivityManager(288): Start proc android.process.acore for content provider com.android.providers.userdictionary/.UserDictionaryProvider: pid=432 uid=10010 gids={50010, 3003, 1015, 1028}
12-20 08:48:43.471: E/StrictMode(377): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
12-20 08:48:43.471: E/StrictMode(377): java.lang.Throwable: Explicit termination method 'close' not called
12-20 08:48:43.471: E/StrictMode(377): at dalvik.system.CloseGuard.open(CloseGuard.java:184)
12-20 08:48:43.471: E/StrictMode(377): at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:412)
12-20 08:48:43.471: E/StrictMode(377): at android.content.res.AssetManager.openNonAssetFdNative(Native Method)
12-20 08:48:43.471: E/StrictMode(377): at android.content.res.AssetManager.openNonAssetFd(AssetManager.java:428)
,並採取這個錯誤在錯誤日誌:
**警告:環境變量家裏沒有設置。以下目錄將用於存儲Git。 警告:未處理的事件循環異常。
警告:EGit無法檢測到原生Git的安裝路徑「gitPrefix」。因此,EGit無法遵守系統級別 可能在**中配置的Git設置
您的圖像保存爲「20131220000000」? HHmmss沒有區別? – Fllo
用1970010102000121.jpg名稱保存。是的。 –