and*_*ryx 18 android photo-gallery
我通过Android 5.0从图库中选择图像遇到问题.我的启动意图的代码是:
private void takePictureFromGallery()
{
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_FROM_FILE);
}
Run Code Online (Sandbox Code Playgroud)
这里是请求代码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
...
}
Run Code Online (Sandbox Code Playgroud)
对于早于5.0的版本,此代码始终可以使用com.android.gallery应用程序.Google相册是Android 5.0上的默认图库应用程序.可能这个问题取决于应用程序还是新的Android操作系统分发问题?
编辑
我理解这个问题:Google相册自动浏览云服务器上备份图片的内容.事实上,如果我关闭每个互联网连接并且在选择图像之后尝试通过@maveň建议实践,则不会通过从InputStream解码Bitmap来获得结果.
所以在这一点上问题变成:android 5.0中有没有办法处理Intent.ACTION_PICK动作,以便系统浏览选择本地设备图库?
and*_*ryx 36
我结合以下方法找到了解决这个问题的方法.这里开始从设备库中选择图像的活动:
private void takePictureFromGallery()
{
startActivityForResult(
Intent.createChooser(
new Intent(Intent.ACTION_GET_CONTENT)
.setType("image/*"), "Choose an image"),
PICK_FROM_FILE);
}
Run Code Online (Sandbox Code Playgroud)
这里处理的意图产生,如在本描述后,注意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;
}
Run Code Online (Sandbox Code Playgroud)
takePictureFromGallery() 被调用 onActivityResult就这样!!
试试这个:
//5.0
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, CHOOSE_IMAGE_REQUEST);
Run Code Online (Sandbox Code Playgroud)
在onActivityResult中使用以下内容:
Uri selectedImageURI = data.getData();
input = c.getContentResolver().openInputStream(selectedImageURI);
BitmapFactory.decodeStream(input , null, opts);
Run Code Online (Sandbox Code Playgroud)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri);
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
} }
Run Code Online (Sandbox Code Playgroud)
tempPath将存储ImageSelected的路径
请查看此内容以获取更多详
| 归档时间: |
|
| 查看次数: |
18577 次 |
| 最近记录: |