Android 图像存储在SD卡ContentResolver

来源:互联网 发布:sem账户优化 编辑:程序博客网 时间:2024/05/09 08:51

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.  }  

 

原创粉丝点击