Mein Ziel ist es, das Bild von ImageView
innen RecyclerView
Das Bild aus dem Internet mit Picasso
libGemeinsame Nutzung eines Bildes von RecyclerView
Ich habe hinzugefügt, unter jedem Artikel eine Share-Taste die Onclick-Funktion richtig gefassten geladen zu teilen, aber kann nicht weiter gehen.
Ich habe diese Datenadapter:
package com.myapp.app;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.myapp.app.model.AndroidVersion;
import com.squareup.picasso.Picasso;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<AndroidVersion> android;
private Context context;
private ImageView content;
public DataAdapter(Context context,ArrayList<AndroidVersion> android) {
this.android = android;
this.context = context;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(android.get(i).getName());
viewHolder.tv_version.setText(android.get(i).getVer());
Picasso.with(context).load(android.get(i).getApi()).into(viewHolder.img_android);
Picasso.with(context).load(android.get(i).getImage()).into(viewHolder.image_android);
viewHolder.btshare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/// button click event
content = (ImageView) v.findViewById(R.id.image_android);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
File cachePath = new File("/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
share.setData(phototUri);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, phototUri);
v.getContext().startActivity(Intent.createChooser(share,"Share via"));
}
});
}
@Override
public int getItemCount() {
return android.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name,tv_version;
private ImageView img_android,image_android;
private Button btshare;
public ViewHolder(View view) {
super(view);
btshare = (Button) view.findViewById(R.id.btshare);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
img_android = (ImageView)view.findViewById(R.id.img_android);
image_android = (ImageView)view.findViewById(R.id.image_android);
}
}
}
Es Kraft schließt die App, wenn ich Feuer Onclick
helfen bitte
Dies ist die Linie Fehlerprotokoll ist:
08-08 21:10:59.956 8934-8934/com.myapp.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.app, PID: 8934
java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.ImageView.setDrawingCacheEnabled(boolean)' on a null object reference
at com.myapp.app.DataAdapter$1.onClick(DataAdapter.java:60)
at android.view.View.performClick(View.java:5232)
at android.view.View$PerformClick.run(View.java:21289)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
können Sie StackTrace auch buchen. –
hinzugefügt die Fehlerzeile – DipanIns
versuchen changing 'content = (ImageView) v.findViewById (R.id.image_android);' von 'content = viewHolder.getImage_android()' weil Sie jetzt für Ihre Bildansicht innerhalb Ihrer Schaltfläche Ansicht suchen –