如何从位图获取Uri对象

在某个点击事件中,我要求用户添加一个图像。 所以我提供了两个select:

  1. 从画廊添加。
  2. 点击相机的新图像。

我的目标是保留与这些图像相关的“uri”列表。

如果用户select图库,那么我得到的图像uri(这很简单)。 但是如果他select了照相机,那么在拍照之后,我得到了该照片的Bitmap对象。

现在,我该如何将该位图对象转换为uri,换句话说,我怎样才能获得该位图对象的相对Uri对象?

谢谢。

我在我的项目中有同样的问题,所以我按照简单的方法( 点击这里 )从位图得到Uri。

public Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } 
 Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); 

上面提到的线创build一个缩略图使用位图,这可能会消耗一些额外的Android设备空间。

这种方法可以帮助你从位图中获取Uri而不消耗一些额外的内存。

 public Uri bitmapToUriConverter(Bitmap mBitmap) { Uri uri = null; try { final BitmapFactory.Options options = new BitmapFactory.Options(); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 100, 100); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, 200, 200, true); File file = new File(getActivity().getFilesDir(), "Image" + new Random().nextInt() + ".jpeg"); FileOutputStream out = getActivity().openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); //get absolute path String realPath = file.getAbsolutePath(); File f = new File(realPath); uri = Uri.fromFile(f); } catch (Exception e) { Log.e("Your Error Message", e.getMessage()); } return uri; } 

我尝试了我在我的评论中提到的post中的以下代码片段..它对我来说工作正常。

 /** * Gets the last image id from the media store * * @return */ private int getLastImageId() { final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA }; final String imageOrderBy = MediaStore.Images.Media._ID + " DESC"; Cursor imageCursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy); if (imageCursor.moveToFirst()) { int id = imageCursor.getInt(imageCursor .getColumnIndex(MediaStore.Images.Media._ID)); String fullPath = imageCursor.getString(imageCursor .getColumnIndex(MediaStore.Images.Media.DATA)); Log.d(getClass().getSimpleName(), "getLastImageId::id " + id); Log.d(getClass().getSimpleName(), "getLastImageId::path " + fullPath); imageCursor.close(); return id; } else { return 0; } } 

在logcat中输出:

 09-24 16:36:24.500: getLastImageId::id 70 09-24 16:36:24.500: getLastImageId::path /mnt/sdcard/DCIM/Camera/2012-09-24 16.36.20.jpg 

我也没有看到上面的代码片段中的任何harcoded名称。 希望这可以帮助。

 Try To Use this May Be Helpful to you : new AsyncTask<Void, Integer, Void>() { protected void onPreExecute() { }; @Override protected Void doInBackground(Void... arg0) { imageAdapter.images.clear(); initializeVideoAndImage(); return null; } protected void onProgressUpdate(Integer... integers) { imageAdapter.notifyDataSetChanged(); } public void initializeVideoAndImage() { final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Thumbnails._ID }; String orderBy = MediaStore.Images.Media._ID; Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy); final String[] videocolumns = { MediaStore.Video.Thumbnails._ID, MediaStore.Video.Media.DATA }; orderBy = MediaStore.Video.Media._ID; Cursor videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videocolumns, null, null, orderBy); int i = 0; int image_column_index = 0; if (imageCursor != null) { image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID); int count = imageCursor.getCount(); for (i = 0; i < count; i++) { imageCursor.moveToPosition(i); int id = imageCursor.getInt(image_column_index); ImageItem imageItem = new ImageItem(); imageItem.id = id; imageAdapter.images.add(imageItem); } } if (videoCursor != null) { image_column_index = videoCursor.getColumnIndex(MediaStore.Video.Media._ID); int count = videoCursor.getCount(); for (i = 0; i < count; i++) { videoCursor.moveToPosition(i); int id = videoCursor.getInt(image_column_index); ImageItem imageItem = new ImageItem(); imageItem.id = id; imageAdapter.images.add(imageItem); } } publishProgress(i); if (imageCursor != null) { image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID); int count = imageCursor.getCount(); for (i = 0; i < count; i++) { imageCursor.moveToPosition(i); int id = imageCursor.getInt(image_column_index); int dataColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA); ImageItem imageItem = imageAdapter.images.get(i); Bitmap img = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null); int column_index = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); imageItem.imagePath = imageCursor.getString(column_index); imageItem.videoPath = ""; try { File imageFile = new File(Environment.getExternalStorageDirectory(), "image" + i); imageFile.createNewFile(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); if (bos != null && img != null) { img.compress(Bitmap.CompressFormat.PNG, 100, bos); } byte[] bitmapData = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(imageFile); fos.write(bitmapData); fos.close(); imageItem.thumbNailPath = imageFile.getAbsolutePath(); try { boolean cancelled = isCancelled(); // if async task is not cancelled, update the // progress if (!cancelled) { publishProgress(i); SystemClock.sleep(100); } } catch (Exception e) { Log.e("Error", e.toString()); } // publishProgress(); } catch (IOException e) { e.printStackTrace(); } // imageAdapter.images.add(imageItem); } } imageCursor.close(); if (videoCursor != null) { image_column_index = videoCursor.getColumnIndex(MediaStore.Video.Media._ID); int count = videoCursor.getCount() + i; for (int j = 0; i < count; i++, j++) { videoCursor.moveToPosition(j); int id = videoCursor.getInt(image_column_index); int column_index = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); ImageItem imageItem = imageAdapter.images.get(i); imageItem.imagePath = videoCursor.getString(column_index); imageItem.videoPath = imageItem.imagePath; System.out.println("External : " + imageItem.videoPath); try { File imageFile = new File(Environment.getExternalStorageDirectory(), "imageV" + i); imageFile.createNewFile(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); MediaMetadataRetriever mediaVideo = new MediaMetadataRetriever(); mediaVideo.setDataSource(imageItem.videoPath); Bitmap videoFiles = mediaVideo.getFrameAtTime(); videoFiles = ThumbnailUtils.extractThumbnail(videoFiles, 96, 96); if (bos != null && videoFiles != null) { videoFiles.compress(Bitmap.CompressFormat.JPEG, 100, bos); } byte[] bitmapData = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(imageFile); fos.write(bitmapData); fos.close(); imageItem.imagePath = imageFile.getAbsolutePath(); imageItem.thumbNailPath = imageFile.getAbsolutePath(); try { boolean cancelled = isCancelled(); // if async task is not cancelled, update the // progress if (!cancelled) { publishProgress(i); SystemClock.sleep(100); } } catch (Exception e) { Log.e("Error", e.toString()); } } catch (IOException e) { e.printStackTrace(); } } } videoCursor.close(); } protected void onPostExecute(Void result) { imageAdapter.notifyDataSetChanged(); }; }.execute(); } 

这是为我工作。 例如以位图的forms从video获取缩略图。 然后我们可以将位图对象转换成uri对象。

 String videoPath = mVideoUri.getEncodedPath(); System.out.println(videoPath); //prints to console the path of the saved video Bitmap thumb = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.MINI_KIND); Uri thumbUri = getImageUri(this, thumb);