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. 【Android(安卓)4.0】Android(安卓)Icon Set的使用
  2. Android(安卓)ImageView 的scaleType 属性图解
  3. Android常见问题总结(五)
  4. android imageview显示不全或者gilde加载不全
  5. android:background="@drawable/"图片拉伸问题
  6. android之图片切圆角
  7. Android(安卓)H5和App交互以及打开图库上传图片并显示
  8. Android之drawable和mipmap目录区别
  9. Android(安卓)开发之Matrix图片处理类的使用

随机推荐

  1. Android(安卓)TextView设置图片技巧
  2. Android studio build inished with non-
  3. 分享android技术牛人博客
  4. Android:解决列表滚动时背景色变黑的方法
  5. Android 文件实现断点上传
  6. ActionBar setDisplayOptions 使用详解
  7. adt-bundle-windows-x86_32-20140702
  8. Android Webview播放HTML5 video的一个思
  9. android 对话框全屏
  10. Android 分享功能