I am trying to display an image into an image view. On the main activity it works.
I would like to create a Navigation drawer, so I created a fragment.
To display my image I use a class EdtTraitment
and the method show()
. This method tries to search a file in a directory. For this I need the Context
of the main activity but, as you will see, I have errors when trying to get this context in the class EdtTraintement
from the fragment.
The fragment code :
public class JourFragment extends Fragment {
public JourFragment() {
// Required empty public constructor
}
Activity mActivity = getActivity();
Context mContext = getActivity();
View view = null;
EdtTraitement edt = new EdtTraitement(mContext,mActivity);
TouchImageView img = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_jour, container, false);
new Thread(new Runnable() {
public void run() {
img = (TouchImageView) view.findViewById(R.id.image_emplois);
edt.showEdt("URL","images.png","URL","text.txt",img); //diplay the img
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return view;
}}
EdtTraitement :
public class EdtTraitement { //constructeur
protected Context mContext;
protected Activity activity;
protected TouchImageView image_edt = null;
protected Bitmap bitmap = null;
public EdtTraitement(Context mContext,Activity _activity) {
activity = _activity;
this.mContext = mContext;
}
public void showEdt(String url_image,String name_img ,String url_md5,String name_md5, TouchImageView img){
image_edt = img;
File img_file = new File(mContext.getFilesDir(), name_img);
if(isOnline()) {
if ((getMd5(url_md5).equals(readFile(name_md5))) && (img_file.exists())) {
image_edt.setImageBitmap(BitmapFactory.decodeFile(img_file.getPath()));
} else {
try {
InputStream inputStream = null;
inputStream = (InputStream) new URL(url_image).getContent();
bitmap = BitmapFactory.decodeStream(inputStream);
image_edt.setImageBitmap(bitmap);
//on le stoque:
storeImage(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
image_edt.setImageBitmap(BitmapFactory.decodeFile(img_file.getPath()));
}
image_edt.setZoom(image_edt.getMaxZoom());
image_edt.setMaxZoom(image_edt.getMaxZoom());
image_edt.setMinZoom(image_edt.getMinZoom());
}}
the log:
04-10 06:54:00.329 11605-11780/oytoch.iut_info E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: oytoch.iut_info, PID: 11605
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at oytoch.iut_info.EdtTraitement.showEdt(EdtTraitement.java:49)
at oytoch.iut_info.JourFragment$1.run(JourFragment.java:38)
at java.lang.Thread.run(Thread.java:761)
line 49:
File img_file = new File(mContext.getFilesDir(), name_img);
It works with no fragment I need this context in other part of code in the class.
I have been unable to find a working solution; how can I change my code to receive my desired outcome?