Android 获取相机在其他活动中保存的图像ID
我有一个使用以下结构的列表: 图片+姓名+小时 描述 起初,我只是简单地使用了一个标准图像,名为nopicture和nopicture2(区别只是指定用户是否注册) 现在,我要做的是拍摄一张照片并显示在这个列表中 要了解列表是如何工作的,下面是代码:Android 获取相机在其他活动中保存的图像ID,android,image,camera,Android,Image,Camera,我有一个使用以下结构的列表: 图片+姓名+小时 描述 起初,我只是简单地使用了一个标准图像,名为nopicture和nopicture2(区别只是指定用户是否注册) 现在,我要做的是拍摄一张照片并显示在这个列表中 要了解列表是如何工作的,下面是代码: public class CustomList extends ArrayAdapter { private final Activity context; private final String[] clientes;
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>
最后,这里是列出用户的“主”应用程序。请注意,我正在将一个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;
}
}
如您所见,列表接收图片的ID,但我找不到任何外部资源项的ID。我只有图像的文件路径。对于这种情况,什么是最好的解决方案
谢谢你,佩德罗。通过intent extra或bundle传递它……我需要在应用程序启动时加载它,并直接从外部资源获取它。
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;
}
}