在其他活动中获取由相机保存的图像ID

时间:2017-02-16 13:54:08

标签: android image camera

我有一个使用以下结构的列表:

图片+名称+小时

描述

一开始我很简单,使用标准的图像,称为nopicture和nopicture2(不同之处仅在于指定用户是否注册)。

现在,我要做的就是拍照并在此列表中显示。

要了解列表的工作原理,请输入以下代码:

public class CustomList extends ArrayAdapter {

    private final Activity context;
    private final String[] clientes;
    private final Integer[] imageId;
    private final String[] descricao;
    private final String[] time;


    public CustomList(Activity context, String[] clientes, Integer[] imageId, String[] descricao, String[] time) {
        super(context, R.layout.list_single, clientes);
        this.context = context;
        this.clientes = clientes;
        this.imageId = imageId;
        this.descricao = descricao;
        this.time = time;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent){
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.nomeCliente);  //atribuindo view do texto ao nome do cliente na view
        TextView txtDesc = (TextView) rowView.findViewById(R.id.desc);
        TextView txtTime = (TextView) rowView.findViewById(R.id.hora);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.foto); //atribuindo view da imagem a imagem do cliente na view

        txtTitle.setText(clientes[position]);
        txtDesc.setText(descricao[position]);
        txtTime.setText(time[position]);
        imageView.setImageResource(imageId[position]);

        return rowView;
        // para entender mais do codigo : https://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html
    }

}

和它的xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<TableRow>
    <ImageView
        android:id="@+id/foto"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="11dp"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp"
        />

    <TableLayout>
        <TableRow>

            <TextView
            android:id="@+id/nomeCliente"
                android:layout_height="25dp"
            android:layout_marginLeft="11dp"
                android:layout_width="220dp" />

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/hora" />
            </TableRow>
        </TableRow>
        <TableRow>
        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_marginLeft="11dp"
            android:textSize="12dp"
            />
        </TableRow>
    </TableLayout>
</TableRow>
</TableLayout>

这是我拍摄照片的活动:

    // CLICK at image -> activates camera
    imgCliente.setOnClickListener(new View.OnClickListener(){
          @Override
          public void onClick(View view) {
              file = new File(Environment.getExternalStorageDirectory().getPath(),getIntent().getExtras().getString("cpfCliente"));
              Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
              cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
              startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
          }
      });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        Bitmap image = null;
        try {
            image = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.fromFile(file));
        }
        catch(FileNotFoundException e){
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        imgCliente.setImageBitmap(image);
    }
}

最后,这里是#34;主要&#34;应用程序列出用户。请注意,我将id传递给列表生成器,我可以从资源中获得的唯一内容是文件的Uri。

if(result.getAsJsonArray().get(result.getAsJsonArray().size()-i-1).getAsJsonObject().get("id_cliente_cliente").getAsJsonObject().get("cpf").getAsString().contains("00000000"))
                                      imageId[i] = R.drawable.nopicture;
                                  else {
                                      Log.v("Path",Environment.getExternalStorageDirectory().getPath()  //file found
                                              +"/"+result.getAsJsonArray().get(result.getAsJsonArray().size()-i-1).getAsJsonObject().get("id_cliente_cliente").getAsJsonObject().get("cpf").getAsString());
                                      if(new File(Environment.getExternalStorageDirectory().getPath()
                                              +"/"+result.getAsJsonArray().get(result.getAsJsonArray().size()-i-1).getAsJsonObject().get("id_cliente_cliente").getAsJsonObject().get("cpf").getAsString()).isFile()){//check if photo exists base on cpf code
                                          Log.v("FILE","found");
                                          File file = new File(Environment.getExternalStorageDirectory().getPath(),
                                                  result.getAsJsonArray().get(result.getAsJsonArray().size()-i-1).getAsJsonObject().get("id_cliente_cliente").getAsJsonObject().get("cpf").getAsString());
                                          Bitmap image = null;
                                          try {
                                              image = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.fromFile(file));
                                          }
                                          catch(FileNotFoundException e9){
                                              e.printStackTrace();
                                          }
                                          catch (IOException e8) {
                                              e.printStackTrace();
                                          }
                                          imageClientes[i] = (ImageView) findViewById(R.id.foto);
                                          imageClientes[i].setImageBitmap(image);
                                          imageId[i] = R.id.foto;
                                      }
                                      else{ //registered but no picture
                                          imageId[i] = R.drawable.nopicture2;
                                      }
                                  }

正如您所看到的,List会收到图片的ID,但我找不到外部资源项的ID。我只有图像的文件路径。什么是这种情况的最佳解决方案?

谢谢佩德罗。

0 个答案:

没有答案