我正在构建一个 Android 应用程序,可以从照片库获取图像。
如果可以的话我有两个问题:
1)
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String action = intent.getAction();
Uri uri = null;
// if this is from the share menu
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM))
{
try
{
// Get resource path from intent callee
uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
我有这个 uri:
内容://media/external/images/media/27031
但是我的代码在这里失败,没有任何信息(它只是崩溃),没有日志猫,没有控制台错误。
try {
if (uri !=null)
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
Log.e(this.getClass().getName(), e.toString());
}
我该如何解决这个问题?
(适用于此网址:Uri uri = Uri.parse("content://media/external/images/media/22719");
2) 我计划将所有 uri 保存在数据结构中。这是推荐的方式吗?
还是将照片保存在本地应用程序文件夹中更好?
更新
我认为问题在于失败的图像位于外部 SD 上,而后续图像位于本地 SD 上。
如何启用从外部 SD 获取图像?
最佳答案
一些用于从照片上的“共享”操作获取位图的代码...
Object obj = null;
if (Intent.ACTION_SEND.equals(_action) && _type != null) {
obj = _bundle.get("android.intent.extra.STREAM");
if( getIntent().getType().startsWith("image/") && obj != null){
if (obj instanceof android.net.Uri){ get((Uri)obj);}}
get(Uri uri){
Bitmap _bit = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri),null,options);
}
关于java - MediaStore.Images.Media.getBitmap 意外失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18793509/