2013-12-19 72 views
0

我只能保存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设置

+0

您的图像保存为“20131220000000”? HHmmss没有区别? – Fllo

+0

用1970010102000121.jpg名称保存。是的。 –

回答

0

在您的代码中,SimpleDateFormat以黄色下划线。你看这皮棉:

要获得本地格式使用getDateInstance(),getDateTimeInstance(),或getTimeInstance(),或使用新的SimpleDateFormat(字符串模板,区域设置区域)与例如Locale.US用于表示ASCII日期。

这意味着,你错过了有正确时间的论点。
this answer,并尝试下面的代码:

String filename; 
Date date = new Date(); 
SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss", java.util.Locale.getDefault()); 
filename = sdf.format(date); 

或列表在这里使用Locale.UK代替java.util.Locale.getDefault(),或者说:Locale Documentation

希望这会有所帮助。

+0

谢谢@Fllo,这段代码解决了我的第一个问题,但仍然对另外一些问题抱有错误。 –

1
Date date = new Date(0); 

此自1970年1月1日00:00:00 UTC创建Date对象,它是0毫秒。

你可能想

Date date = new Date(); 

其创建与当前日期和时间Date对象。


的例外logcat的java.lang.Throwable: Explicit termination method 'close' not called是因为一些resason你没有关闭已打开的文件。至少你应该改变

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(); 
} 

喜欢的东西

FileOutputStream fo = null; 
try { 
    file.createNewFile(); 
    fo = new FileOutputStream(file); 
    // 5 
    fo.write(bytes.toByteArray()); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} finally { 
    if (fo != null) { 
     try { fo.close(); } catch (IOException ignore) {} 
    } 
} 

所以FileOutputStream是任何异常,无论关闭。

+0

当我使用=>日期日期=新日期(); 日期用红色标出。并给出错误:_构造函数Date()是undefined_ –

+1

@ gk91确保您导入'java.util.Date'而不是例如'java.sql.Date' – laalto