Android 图像存储在SD卡ContentResolver
来源:互联网 发布:sem账户优化 编辑:程序博客网 时间:2024/05/09 08:51
Android 图像存储在SD卡ContentResolver
2010年08月06日 下午 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. }
- Android 图像存储在SD卡ContentResolver
- android通过contentResolver删除SD卡上的媒体文件
- Android SD卡存储
- Android存储-sd卡存储
- Android 图片在SD卡及包下的存储
- android中把文件存储在SD卡中
- Android SD卡文件存储
- android-SD卡存储信息
- android开发 SD卡存储
- Android外部sd卡存储
- Android SD卡文件存储
- Android SD卡存储图片
- Android 数据存储---外部存储(SD卡)
- android数据存储ContentProvider数据获取ContentResolver
- (android 基础知识之数据存储) ContentResolver
- 8 把文件存储在SD卡
- android操作文件,保存在手机自带存储上和保存在SD卡上
- stm32f4中用SD卡存储DCMI的图像
- Java 不再是 性能之王(构思中)
- 深入了解SQL Server动态管理视图
- 图像前景--论坛一牛人所写,希望能给大家带来些感触
- android框架开发和移植
- TCP/IP 配置参数
- Android 图像存储在SD卡ContentResolver
- window下dump文件的调试
- strtok 函数笔记
- 设计模式之Prototype(原型模式)
- Window Workflow Foundation 学习笔记(入门)
- C++复习 类
- 设计模式之Adapter(适配器)
- 如何处理Oracle中的坏块
- 软件开发七宗罪