在android 5.0中从图库中选择照片
我在使用安卓5.0从图库中挑选图像时遇到了一个问题。我的启动意图代码是:在android 5.0中从图库中选择照片,android,photo-gallery,Android,Photo Gallery,我在使用安卓5.0从图库中挑选图像时遇到了一个问题。我的启动意图代码是: private void takePictureFromGallery() { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); startActivityForResult(intent, PICK_FROM_FILE); } 这里是在onActivityResul
private void takePictureFromGallery()
{
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_FROM_FILE);
}
这里是在onActivityResult()方法中调用的函数,用于请求代码从_文件中拾取_
private void handleGalleryResult(Intent data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
// field declaration private String mTmpGalleryPicturePath;
mTmpGalleryPicturePath = cursor.getString(columnIndex);
cursor.close();
// at this point mTmpGalleryPicturePath is null
...
}
对于5.0之前的版本,此代码始终可以使用com.android.gallery应用程序工作。谷歌照片是安卓5.0上的默认图库应用程序。
这个问题可能取决于应用程序,还是新的android操作系统发行版的问题
编辑
我理解这个问题:谷歌照片会在云服务器上自动浏览其备份图像的内容。事实上,@maveň尝试了pratice的建议,如果我关闭了每个internet连接,然后选择了一个图像,它不会从InputStream解码位图得到结果
所以在这一点上,问题变成了:安卓5.0中有没有一种方法来处理Intent.ACTION\u PICK操作,以便系统在本地设备图像库中浏览选择 试试这个:
//5.0
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, CHOOSE_IMAGE_REQUEST);
在onActivityResult中使用以下命令:
Uri selectedImageURI = data.getData();
input = c.getContentResolver().openInputStream(selectedImageURI);
BitmapFactory.decodeStream(input , null, opts);
更新 tempPath将存储所选图像的路径
有关更多详细信息,我结合以下方法找到了此问题的解决方案。 在此处开始从设备库中拾取图像的活动:
private void takePictureFromGallery()
{
startActivityForResult(
Intent.createChooser(
new Intent(Intent.ACTION_GET_CONTENT)
.setType("image/*"), "Choose an image"),
PICK_FROM_FILE);
}
这里要处理意图的结果,如本文所述,请注意,getPath()
函数的工作方式与android构建版本不同:
private void handleGalleryResult(Intent data)
{
Uri selectedImage = data.getData();
mTmpGalleryPicturePath = getPath(selectedImage);
if(mTmpGalleryPicturePath!=null)
ImageUtils.setPictureOnScreen(mTmpGalleryPicturePath, mImageView);
else
{
try {
InputStream is = getContentResolver().openInputStream(selectedImage);
mImageView.setImageBitmap(BitmapFactory.decodeStream(is));
mTmpGalleryPicturePath = selectedImage.getPath();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@SuppressLint("NewApi")
private String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor;
if(Build.VERSION.SDK_INT >19)
{
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, sel, new String[]{ id }, null);
}
else
{
cursor = getContentResolver().query(uri, projection, null, null, null);
}
String path = null;
try
{
int column_index = cursor
.getColumnIndex(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index).toString();
cursor.close();
}
catch(NullPointerException e) {
}
return path;
}
从活动结果的takePictureFromGallery()
调用
就这些 可以,但我如何检索照片的字符串路径?我需要它@和.ryx照片的
字符串路径是什么意思?
?例如,如果文件“picture01.jpg”存储在“sdcard”目录的“Photos”文件夹中,我的字符串输出应该是:“/sdcard/Photos/picture01.jpg”,因此图像文件的路径…路径是不固定的,它会随着设备的不同而变化。像在一些设备中一样,它将是/sdcard/Photos/picture01.jpg
,而在其他设备中则是/sdcard0/Photos/picture01.jpg
@and.ryx调试并检查字符串的值
…我给出的代码对我来说工作正常,在设备上测试此代码。很抱歉,但仍然不理解。。。在onResult()中调用的getPath()方法是用方法编写的(在本例中,您的参数语法错误),还是由您自己编写的函数?不管怎样,我试图获得链接中所示的路径,但没有使用BitmapFactory解码流,但不起作用。我认为这不正确。应该是如果(Build.VERSION.SDK_INT>19)而不是如果(Build.VERSION.SDK_INT>=19)我得到了IllegalArgumentException:无效URI:content://com.google.android.apps.photos.contentprovider/0/1/mediaKey%3A...6mkQk-P4ZI/ACTUAL/11…80用于在线存储在谷歌照片中的照片。很好的解决方案在Andorid 5.0中适用于我,从其他文件夹复制图像。
private void handleGalleryResult(Intent data)
{
Uri selectedImage = data.getData();
mTmpGalleryPicturePath = getPath(selectedImage);
if(mTmpGalleryPicturePath!=null)
ImageUtils.setPictureOnScreen(mTmpGalleryPicturePath, mImageView);
else
{
try {
InputStream is = getContentResolver().openInputStream(selectedImage);
mImageView.setImageBitmap(BitmapFactory.decodeStream(is));
mTmpGalleryPicturePath = selectedImage.getPath();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@SuppressLint("NewApi")
private String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor;
if(Build.VERSION.SDK_INT >19)
{
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, sel, new String[]{ id }, null);
}
else
{
cursor = getContentResolver().query(uri, projection, null, null, null);
}
String path = null;
try
{
int column_index = cursor
.getColumnIndex(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index).toString();
cursor.close();
}
catch(NullPointerException e) {
}
return path;
}