我onActivityResult从mediastore图像选择返回,我可以使用以下内容获取图像的URI:
Uri selectedImage = data.getData();
Run Code Online (Sandbox Code Playgroud)
将其转换为字符串可以得到:
content://media/external/images/media/47
Run Code Online (Sandbox Code Playgroud)
或者路径给出:
/external/images/media/47
Run Code Online (Sandbox Code Playgroud)
但是我似乎无法找到将其转换为绝对路径的方法,因为我想将图像加载到位图而不必将其复制到某处.我知道这可以使用URI和内容解析器完成,但这似乎在重新启动手机时中断,我想MediaStore在重新启动之间不会保持其编号相同.
在Android 4.4(KitKat)中访问新图库之前,我使用此方法在SD卡上获得了真正的路径:
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
startManagingCursor(cursor);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Run Code Online (Sandbox Code Playgroud)
现在,Intent.ACTION_GET_CONTENT返回不同的数据:
之前:
content://media/external/images/media/62
Run Code Online (Sandbox Code Playgroud)
现在:
content://com.android.providers.media.documents/document/image:62
Run Code Online (Sandbox Code Playgroud)
我怎么能设法获得SD卡上的真实路径?
当我将媒体库中的图像加载到位图时,一切都运行正常,除了在垂直拿着手机时用相机拍摄的图片被旋转,以便我总是得到水平图片,即使它在画廊.为什么这样,我怎么能正确加载它?
我正在构建一个使用相机拍照的应用程序.这是我的源代码:
File file = new File(Environment.getExternalStorageDirectory(),
imageFileName);
imageFilePath = file.getPath();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, ACTIVITY_NATIVE_CAMERA_AQUIRE);
Run Code Online (Sandbox Code Playgroud)
在onActivityResult()方法上,我BitmapFactory.decodeStream()用来拾取图像.
当我在Nexus上运行我的应用程序时,它运行良好.但是当我在三星Galaxy S或HTC Inspire 4G上运行时,图像的方向是不正确的.

拍摄后的图像预览--------- SD卡上的真实图像
拍摄后的图像预览--------- SD卡上的真实图像
我正在尝试控制Android相机在肖像应用程序中拍照,但是当我保存图片时,它是在风景中.我用setCameraDisplayOrientation()方法将图像旋转了90个等级 ,但是不起作用.
然后我发现这个职位,但TAG_ORIENTATIONIS 0(不确定).如果我捕获此值并应用旋转值,则也不起作用.
我如何拍摄肖像并以良好的方向保存?
/** Initializes the back/front camera */
private boolean initPhotoCamera() {
try {
camera = getCameraInstance(selected_camera);
Camera.Parameters parameters = camera.getParameters();
// parameters.setPreviewSize(width_video, height_video);
// parameters.set("orientation", "portrait");
// parameters.set("rotation", 1);
// camera.setParameters(parameters);
checkCameraFlash(parameters);
// camera.setDisplayOrientation( 0);
setCameraDisplayOrientation(selected_camera, camera);
surface_view.getHolder().setFixedSize(width_video, height_video);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width_video, height_video);
surface_view.setLayoutParams(lp);
camera.lock();
surface_holder = surface_view.getHolder();
surface_holder.addCallback(this);
surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setPreviewCamera();
} catch (Exception e) {
Log.v("RecordVideo", "Could not initialize the Camera");
return false;
}
return true; …Run Code Online (Sandbox Code Playgroud) 我想让用户从图库中选择个人资料图片.我的问题是有些图片是向右旋转的.
我像这样启动图像选择器:
Intent photoPickerIntent = new Intent();
photoPickerIntent.setType("image/*");
photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(photoPickerIntent, "Select profile picture"), Global.CODE_SELECT_PICTURE);
Run Code Online (Sandbox Code Playgroud)
我从onActivityResult获取图像,如下所示:
Uri selectedPicture = data.getData();
profilePic = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), selectedPicture);
Run Code Online (Sandbox Code Playgroud)
如何让图像不被旋转?
更新:
根据我收到的一些有用的答案,我设法提出了以下工作解决方案(这只是一个工作代码,写得不好).我很想得到你如何改进它的反馈!
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == Global.CODE_SELECT_PICTURE) {
// Get selected gallery image
Uri selectedPicture = data.getData();
// Get and resize profile image
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = activity.getContentResolver().query(selectedPicture, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex); …Run Code Online (Sandbox Code Playgroud) 你如何Bitmap变成一个InputStream?
我想用它InputStream作为ETC1Util.loadTexture()函数的输入.
在处理ACTION_IMAGE_CAPTURE活动时我遇到了方向问题.我已经使用过,TAG_ORIENTATION所以我会相应地旋转图片.但现在我们发现在一些较新的设备上这不起作用.事实上,它为所有方向返回1.
这是我们观察到的设备列表;
有趣的是,一旦这个图像是图库它正确显示,如果我选择它,TAG_ORIENTATION就会正确填充.所以不知何故OS正确地填写了这些信息,但没有ActivityResult.
确定方向的最可靠方法是什么?有人在另一个问题上建议比较高度和宽度但是在获得这些时,它们会根据方向正确切换(另一个谜)
编辑:似乎这可能连接到另一个错误,其中操作系统复制在库中拍摄的图像(它只应该将图像保存在我们指定的URL中),事情是这个图像在库中有ORIENTATION信息而指定位置的一个没有.
这是错误; http://code.google.com/p/android/issues/detail?id=19268
编辑2:我已经向Android提交了一个新的错误.我很确定这是与上述错误相关的操作系统错误. http://code.google.com/p/android/issues/detail?id=22822
我看了一下,但似乎没有一个坚实的答案/解决方案,非常恼人的问题.
我以纵向拍摄照片,当我点击保存/放弃时,按钮的方向也正确.问题是,当我稍后检索图像时,它是横向的(图片已逆时针旋转90度)
我不想强迫用户以特定方向使用相机.
有没有办法可以检测照片是以纵向模式拍摄,然后解码位图并以正确的方式翻转它?
我已经浏览了一些链接,以获得从默认图库中选择的图像的正确图像方向,以便在exif标记始终返回0的所有设备中标准化.
对于使用肖像相机应用程序android拍摄的图像,EXIF方向标记值始终为0
http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/
如何获得适用于所有设备的精确解决方案?
android ×10
orientation ×2
uri ×2
bitmap ×1
exif ×1
gallery ×1
image ×1
mediastore ×1
path ×1
rotation ×1