我在图像恢复方面遇到问题。我不知道如何从recyclerViewAdapter
发送到activity
,以及如何从activity
发送图像到fragment
。使用文本或数字数据没有问题,问题在于位图数据。
我从phpmysql恢复数据:
if($consulta){
if($reg=mysqli_fetch_array($resultado)){
$result["nombre"]=$reg['nombre'];
$result["color"]=$reg['color'];
$result["texto1"]=$reg['texto1'];
$result["texto2"]=$reg['texto2'];
$result["texto3"]=$reg['texto3'];
$result["texto4"]=$reg['texto4'];
$result["precio"]=$reg['precio'];
$result["ancho"]=$reg['ancho'];
$result["largo"]=$reg['largo'];
$result["informacion"]=$reg['informacion'];
$result["imagen"]=base64_encode($reg['foto']);
$json['datos'][]=$reg;
}
mysqli_close($conexion);
echo json_encode($json);
}
从Android中的PHP恢复数据:
@Override
public void onResponse(JSONObject response) {
//Toast.makeText(getContext(),"Conexion",Toast.LENGTH_SHORT).show();
ListaArticulosPOJO miLista=null;
JSONArray jsonArray = response.optJSONArray("datos");
try{
for (int i=0;i<jsonArray.length();i++){
miLista= new ListaArticulosPOJO();
JSONObject jsonObject=null;
jsonObject = jsonArray.getJSONObject(i);
miLista.setNombre(jsonObject.optString("nombre"));
miLista.setColor(jsonObject.optString("color"));
miLista.setTexto1(jsonObject.optString("texto1"));
miLista.setTexto2(jsonObject.optString("texto2"));
miLista.setTexto3(jsonObject.optString("texto3"));
miLista.setTexto4(jsonObject.optString("texto4"));
miLista.setPrecio(jsonObject.optDouble("precio"));
miLista.setAncho(jsonObject.optDouble("ancho"));
miLista.setLargo(jsonObject.optDouble("largo"));
miLista.setGrueso(jsonObject.optDouble("grueso"));
miLista.setInformacion(jsonObject.optString("informacion"));
miLista.setDatos(jsonObject.optString("fotoArticulo"));
list.add(miLista);
recuperaActivity=miLista.getNombre().toString();
}
}
catch (Exception e){
e.printStackTrace();
}
RecyclerViewListaArticulosAdapter adapter=new RecyclerViewListaArticulosAdapter(list,getContext());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
}
我将数据存储在RecyclerView中的Intent中:
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.id.setText(mDatos.get(position).getNombre());
holder.color.setText(mDatos.get(position).getColor());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(mContext,ArticulosActivity.class);
Bundle bundle= new Bundle();
intent.putExtra("nombre",mDatos.get(position).getNombre());
intent.putExtra("color",mDatos.get(position).getColor());
intent.putExtra("texto1",mDatos.get(position).getTexto1());
intent.putExtra("texto2",mDatos.get(position).getTexto2());
intent.putExtra("texto3",mDatos.get(position).getTexto3());
intent.putExtra("texto4",mDatos.get(position).getTexto4());
intent.putExtra("precio",mDatos.get(position).getPrecio());
intent.putExtra("ancho",mDatos.get(position).getAncho());
intent.putExtra("largo",mDatos.get(position).getLargo());
intent.putExtra("grueso",mDatos.get(position).getGrueso());
//intent.putExtra("foto",mDatos.get(position).getFotoArticulo());
intent.putExtra("informacion",mDatos.get(position).getInformacion()); intent.putExtra("foto",mDatos.get(position).getFotoArticulo());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
}
});
}
我恢复了活动中的数据:
static String nombre;
static String color;
static String texto1;
static String texto2;
static String texto3;
static String texto4;
static double precio;
static double ancho;
static double largo;
static double grueso;
static String fotoArticulo;
static String informacion;
static ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_articulos);
toolbar = (Toolbar)findViewById(R.id.toolbar_activity_articulos);
setSupportActionBar(toolbar);
Intent intent=getIntent();
nombre=intent.getExtras().getString("nombre");
color=intent.getExtras().getString("color");
texto4=intent.getExtras().getString("texto4");
ancho=intent.getExtras().getDouble("ancho");
largo=intent.getExtras().getDouble("largo");
grueso=intent.getExtras().getDouble("grueso");
precio=intent.getExtras().getDouble("precio");
informacion=intent.getExtras().getString("informacion");
fotoArticulo=intent.getExtras().getString("foto");
//imageView=intent.getExtras().("foto");
android.support.v4.app.FragmentManager fmLista1= getSupportFragmentManager();
fmLista1.beginTransaction().replace((R.id.txt_base_articulos_izquierda),new ArticulosListaFragment()).commit();
android.support.v4.app.FragmentManager fmLista2= getSupportFragmentManager();
fmLista2.beginTransaction().replace((R.id.txt_base_articulos_derecha),new ArticulosDatosFragment()).commit();
}
我从片段中调用数据并显示给他们:
public ArticulosDatosFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista=inflater.inflate(R.layout.fragment_articulos_datos, container, false);
articulosActivity =new ArticulosActivity();
radioGroup=(RadioGroup)vista.findViewById(R.id.txt_radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId==R.id.txt_radio_eh){
Toast.makeText(getContext(),"EH Pulsado ",Toast.LENGTH_SHORT).show();
}
else if(checkedId==R.id.txt_radio_gh){
Toast.makeText(getContext(),"GH Pulsado ",Toast.LENGTH_SHORT).show();
}
}
});
*imageView=(ImageView)vista.findViewById(R.id.txt_imagen_articulo);
//imageView.setImageBitmap(articulosActivity.fotoArticulo);*
informacion=(TextView)vista.findViewById(R.id.txt_articulo_informacion);
informacion.setText(articulosActivity.informacion);
texto1=(TextView)vista.findViewById(R.id.txt_text1);
texto1.setText(articulosActivity.nombre);
texto2=(TextView)vista.findViewById(R.id.txt_text2);
texto2.setText(articulosActivity.color);
texto3=(TextView)vista.findViewById(R.id.txt_text3);
texto3.setText(articulosActivity.largo+" x "+articulosActivity.ancho+" x "+articulosActivity.grueso);
texto4=(TextView)vista.findViewById(R.id.txt_text4);
texto4.setText(articulosActivity.texto4);
m2=(TextView)vista.findViewById(R.id.txt_precio_metro);
m2.setText(articulosActivity.precio+"");
bedarf=(EditText)vista.findViewById(R.id.txt_cliente_metro);
boton=(Button)vista.findViewById(R.id.txt_boton_kalkulation);
boxPrecio=(TextView)vista.findViewById(R.id.txt_precio_box);
boxTotal=(TextView)vista.findViewById(R.id.txt_metros_total);
menge=(TextView)vista.findViewById(R.id.txt_metros_total);
summe=(TextView)vista.findViewById(R.id.txt_precio_total);
return vista;
}
如何以与恢复其他数据相同的方式恢复位图类型图像。预先感谢您的帮助。
答案 0 :(得分:0)
您可以使用图像路径并按照@Android Team的说明加载它,否则您可以将该位图转换为字节数组,然后通过intent.putExtra将其传递给intent并进行检索。
您可以尝试使用以下代码进行位图转换:
Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bmp.recycle();
答案 1 :(得分:0)
我从您的问题中得到的是,您想通过json加载在base64中获得的图像,所以我会回答。
由于您收到的base64是String,因此假设它是enryptedImageString。
public static Bitmap getBitmapFromEncodedString(String enryptedImageString) {
if (enryptedImageString != null && !enryptedImageString.equals("")) {
byte[] decodedString = Base64.decode(enryptedImageString, Base64.DEFAULT);
Bitmap decodeBitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return decodeBitmap;
} else
return null;
}
此方法将返回base64字符串图像的位图。
现在您的片段可能具有以下内容:
imageView=(ImageView)vista.findViewById(R.id.txt_imagen_articulo);
imageView.setImageBitmap(getBitmapFromEncodedString(articulosActivity.fotoArticulo));
请先检查该方法的空条件。
另外,我不鼓励创建活动对象,相反,如果片段正确连接,则应尝试使用getActivity()。
您应该避免图像完全传输,因为在某些情况下图像可能会很大,如果可能的话,通过托管它来传递URL。 并使用Glide或Picasso库直接在imageview中显示。