【问题标题】:Displaying photo captured using 3rd party android application in my application在我的应用程序中显示使用 3rd 方 android 应用程序拍摄的照片
【发布时间】:2012-10-16 08:04:38
【问题描述】:

我无法显示使用第 3 方 Android 应用程序捕获的当前图像并将其显示在我自己的本机应用程序上。它总是一个只有操作栏的空屏幕。但是,拍摄的照片可能位于我指定的文件夹中。

有人可以帮忙吗?不胜感激!干杯:)

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:id="@+id/linear">

  <ImageView android:id="@+id/imageView1"
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:contentDescription="@string/desc"/>
</LinearLayout>

源代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch(requestCode) {
    case 0:
        if(resultCode == Activity.RESULT_OK){  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }
    break; 

    case 1:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = data.getData();
            imageView.setImageURI(selectedImage);
        }
    break;

    }
  }
}

【问题讨论】:

  • 第 3 方应用程序的图像存储在哪里?
  • 嗨,我已将它指定在 SD 卡上 -> 图片 -> MyCreatedFolder
  • 然后就可以从sdcard获取图片文件名,放到ImageView中了。就这么简单?您遇到的问题在哪里
  • 嗨阿米特,我只是想知道我是否可以让上面的代码工作。在我捕获图像后它显示一个空白屏幕。
  • 那么您是在选择相册然后放置图像吗??

标签: android android-layout android-intent android-emulator


【解决方案1】:
android:contentDescription="@string/desc"

将上面的行添加到 ImageView 的 xml 中,其中 desc 应该在 string.xml 中并将其设置为某个值。 看起来这定义了简要描述视图内容的文本。此属性主要用于可访问性。由于某些视图没有文本表示,因此此属性可用于提供此类。

像 ImageViews 和 ImageButtons 这样的非文本小部件应该使用 contentDescription 属性来指定小部件的文本描述,以便屏幕阅读器和其他辅助工具可以充分描述用户界面。

String  selectedImagePath = getPath(selectedImageUri);
Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
imageView.setImageBitmap(bmp);

使用上面的代码

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

【讨论】:

  • 嗨,阿米特。那不是我真正想要的。我想要的是显示我刚刚从当前意图中捕获的照片。同时,我的图像视图“[Accessibility] Missing contentDescription attribute on image”出现此错误。你能帮忙吗?我现在拥有的是一个空白屏幕,在捕获图像后只有一个操作栏。我希望通过图像视图显示我刚刚拍摄的照片。谢谢!!!
  • 嗨阿米特,我已经编辑了我的帖子并附上了我拥有的完整源代码。
【解决方案2】:

您是否将fileUri 的值保存在您的Activity 中?如果是这样,您是从保存的实例中恢复它吗?

如果不是,它可能会在 Activity 被销毁时被重置。

【讨论】:

    猜你喜欢
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多