【问题标题】:How to capture and display image in custom listview如何在自定义列表视图中捕获和显示图像
【发布时间】:2017-05-02 06:14:12
【问题描述】:

我有一个自定义列表视图,其中包括文本视图、复选框和图像视图。如果选中复选框,则 textview 和 imageview 将可见。当我单击 textView 时,我会拍照并在 imageview 中显示。因为我不能在适配器中调用 onActivityResult,所以我在 Activity 中调用它,我想将 listView 中对象的位置从适配器发送到活动,但它发送一个错误,我在 MainActivity 中收到的位置为空。那么,我该如何解决呢?

这是我的 .xml 文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingRight="10dp"
android:paddingLeft="16dp"
android:orientation="horizontal">


<TextView
    android:id="@+id/tv_instore"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif"
    android:layout_centerVertical="true"
    android:text="Product"
    android:textSize="14sp"
    android:layout_weight="1"/>
<CheckBox
    android:id="@+id/chk_instore"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_weight="1"
    android:checked="false"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:paddingTop="5dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Capture"
        android:visibility="invisible"
        android:id="@+id/txtCapture"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/img1"
        android:layout_weight="1"
        android:paddingTop="5dp"
        android:visibility="invisible"/>
</LinearLayout>

这是在适配器中:

txtCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mContext.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
               Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
               i.putExtra("position",position);
               ((Activity) mContext).startActivityForResult(i, 100);
            }
        }
});

这是我在 MainActivity 中的 onActivityResult():

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==100&&resultCode==RESULT_OK){
        Bitmap b = (Bitmap)data.getExtras().get("data");
        int position = (int)data.getExtras().get("position");
        POSMQty p = arrPOSMInstore.get(position); 
        p.setBitmap(b);
    }
}

【问题讨论】:

    标签: android image android-intent bitmap capture


    【解决方案1】:

    您在活动结果中获得的意图与您在 startActivityForResult() 中传递的意图不同。这就是你没有得到职位的原因。

    为此,您可以将位置保存在活动中并在 onActivityResult() 中使用。

    【讨论】:

      【解决方案2】:

      问题是您发送打开相机活动的意图与您在活动结果中接收的意图完全不同。 Intent 接收由 Camera Activity 创建,用于将图像数据发送到您的 Activity。

      这是一种解决方案。

      将适配器中的变量声明为单击位置

      private int posClicked = -1;
      

      在你的

      txtCapture.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (mContext.getPackageManager().hasSystemFeature(
                      PackageManager.FEATURE_CAMERA)) {
                     Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    // i.putExtra("position",position);
                    posClicked = position;
                     ((Activity) mContext).startActivityForResult(i, 100);
                  }
              }
      });
      

      然后在活动结果之后在您的适配器中创建一个方法

      public void afterActivityResult(Bitmap bmp,.. extra){
        //do the need full
      }
      

      在您的活动结果中

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if(requestCode==100&&resultCode==RESULT_OK){
              Bitmap b = (Bitmap)data.getExtras().get("data");
              int position = (int)data.getExtras().get("position");
              POSMQty p = arrPOSMInstore.get(position); 
              p.setBitmap(b);
              yourAdapter.afterActivityResult(...);
      

      } }

      您也可以为 clickedPosition 方法制作 getter 方法。

      在您的适配器中

      public int getClickedPosition(){
           return posClicked;
      }
      

      并像使用它

      adapter.getClickedPosition()
      

      在你的活动中。

      【讨论】:

      • 那么,我可以在我的活动中获得位置吗?
      • 为您编辑了我的答案。如果有帮助,请点赞并标记正确。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多