Android 图像存储在SDContentResolver

20100806 下午 03:27

关于Android 拍照程序保存图片问题,用程序拍摄到图片以后,直接保存在SD卡里,但是无法在相册中预览和查看,必须使用以下方式,才能把图片加载到系统相册中。


1.
把图片放到SD卡的相机默认保存目录中:

1. public class ImageManager {

2. public static Uri addImageAsCamera(ContentResolver cr, Bitmap bitmap) {

3. long dateTaken = System.currentTimeMillis();

4. String name = createName(dateTaken) + ".jpg";

5. String uriStr = MediaStore.Images.Media.insertImage(cr, bitmap, name,

6. null);

7. return Uri.parse(uriStr);

8. }

9.

10. private static String createName(long dateTaken) {

11. return DateFormat.format("yyyy-MM-dd_kk.mm.ss", dateTaken).toString();

12. }

13. }

2.指定文件卡,放入图片,并能够预览。

1. private static final String TAG = "ImageManager";

2. private static final String APPLICATION_NAME = "PATOM";

3. private static final Uri IMAGE_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

4. private static final String PATH = Environment.getExternalStorageDirectory().toString() + "/" + APPLICATION_NAME;

5.

6. public static Uri addImageAsApplication(ContentResolver cr, Bitmap bitmap) {

7. long dateTaken = System.currentTimeMillis();

8. String name = createName(dateTaken) + ".jpg";

9. return addImageAsApplication(cr, name, dateTaken, PATH, name, bitmap, null);

10. }

11.

12. public static Uri addImageAsApplication(ContentResolver cr, String name,

13. long dateTaken, String directory,

14. String filename, Bitmap source, byte[] jpegData) {

15.

16. OutputStream outputStream = null;

17. String filePath = directory + "/" + filename;

18. try {

19. File dir = new File(directory);

20. if (!dir.exists()) {

21. dir.mkdirs();

22. Log.d(TAG, dir.toString() + " create");

23. }

24. File file = new File(directory, filename);

25. if (file.createNewFile()) {

26. outputStream = new FileOutputStream(file);

27. if (source != null) {

28. source.compress(CompressFormat.JPEG, 75, outputStream);

29. } else {

30. outputStream.write(jpegData);

31. }

32. }

33.

34. } catch (FileNotFoundException ex) {

35. Log.w(TAG, ex);

36. return null;

37. } catch (IOException ex) {

38. Log.w(TAG, ex);

39. return null;

40. } finally {

41. if (outputStream != null) {

42. try {

43. outputStream.close();

44. } catch (Throwable t) {

45. }

46. }

47. }

48.

49. ContentValues values = new ContentValues(7);

50. values.put(Images.Media.TITLE, name);

51. values.put(Images.Media.DISPLAY_NAME, filename);

52. values.put(Images.Media.DATE_TAKEN, dateTaken);

53. values.put(Images.Media.MIME_TYPE, "image/jpeg");

54. values.put(Images.Media.DATA, filePath);

55. return cr.insert(IMAGE_URI, values);

56. }

更多相关文章

  1. .Net程序员玩转Android开发---(12)ListView显示数据
  2. Android控件之利用selector自定义的带文字的图片按钮
  3. 谈Android下一个apk安装多个程序入口图标
  4. Android 开发之Matrix图片处理类的使用
  5. Android H5和App交互以及打开图库上传图片并显示
  6. android之图片切圆角
  7. 以一个小程序设计来入门Android
  8. 应用组件-应用程序介绍

随机推荐

  1. Ubuntu16.04下安装pip
  2. 互联网架构设计之Redis篇-【Redis Window
  3. Centos下安装破解Jira7的操作记录
  4. 微信二维码支付native原生支付开发模式二
  5. 信息安全第一篇(加密算法介绍)
  6. Linux硬盘挂载
  7. Java项目Centos7发布之发布过程
  8. 信息安全第二篇(加密算法对比)
  9. 运维安全之Tripwire
  10. 信息安全第三篇(网络传输的加密与解密)