作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前遇到了以下问题:当我想从图库中检索图像时,我使用以下代码启动图库的 Intent 。
public void useGallery() {
this.intentbasedleave=true;
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, getString(R.string.pleaseselect_image)), IMAGE_PICK);
}
当我从图库中获取数据时,我使用此方法:
private void imageFromGallery(int resultCode, 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]);
String filePath = cursor.getString(columnIndex);
cursor.close();
this.updateImageView(BitmapFactory.decodeFile(filePath));
}
这有效,除非所选图片来自 Google+ 或即时上传。那么 BitmapFactory.decodeFile(filePath)) 似乎是空的?因为该方法引发空指针异常。
因此,我的问题是:如何使用来自 Google+ 和图库中即时上传的图片?
最佳答案
使用 BitmapFactory.decodeUri
而不是 BitmapFactory.decodeFile
。
您可以将您的方法 imageFromGallery
简化为
private void imageFromGallery(int resultCode, Intent data) {
Uri selectedImage = data.getData();
updateImageView(BitmapFactory.decodeUri(getContext().getContentResolver(), selectedImage));
}
(假设您可以从某处访问上下文)。
关于android - 在我的应用程序中即时上传和 Google+ 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13625049/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!