Android拍照

一、Uri 

     通用资源标志符(Universal Resource Identifier, 简称"URI")。

  Uri代表要操作的数据,Android上可用的每种资源 - 图像、视频片段等都可以用Uri来表示。

  URI一般由三部分组成:

    访问资源的命名机制。 

    存放资源的主机名。 

    资源自身的名称,由路径表示。 

  Android的Uri由以下三部分组成: "content://"、数据的路径、标示ID(可选)

  举些例子,如: 

    所有联系人的Uri: content://contacts/people

    某个联系人的Uri: content://contacts/people/5

    所有图片Uri: content://media/external

    某个图片的Uri:content://media/external/images/media/4

二、相互转换

1.从Uri获得文件路径

 1 private String getFilePathByContentResolver(Uri uri) {
 2         if (null == uri) {
 3             return null;
 4         }
 5         Cursor c = context.getContentResolver().query(uri, null, null, null, null);
 6         String filePath = null;
 7         try {
 8             if (c != null && c.moveToFirst()) {
 9                 filePath = c.getString(c
10                         .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
11             }
12         } finally {
13             if (c != null) {
14                 c.close();
15             }
16         }
17         return filePath;
18     }

2、从文件路径获得Uri

如果Uri是从相册取出的,需转换成Bitmap,重新转换至Uri,如下:

 1          try {
 2             //为防止OOM,设置BitmapFactory.Options来获取bitmap
 4                      BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
 5                      bitmapOptions.inSampleSize = 4;
 6                      Bitmap bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(originalUri), null , bitmapOptions);
 7                      bitmap = Bitmap.createScaledBitmap(bitmap, 640, 640, false);
 8                      Uri Uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null)); 
10               if (!bitmap.isRecycled()) {
11                   bitmap.recycle();
12                    bitmap = null;
13                }
14             } catch (FileNotFoundException e) {
15                  e.printStackTrace();
16             } catch (IOException e) {
17                  e.printStackTrace();
18             }

 

posted @ 2015-01-27 15:55  Lemon_SG  阅读(208)  评论(0)    收藏  举报