Create getContentResolver () to work in class extending fragment class
I mean to use two snippets, first to display the contact list and the second to display the details of the contact that is selected in the top snippet.
The class I am using to extend the Fragment class does not allow me to use the method getContentResolver()
due to a context issue. Now I'm trying to find a contact in the same class that extends the Fragment class and uses it to display as a list and its details. After going through some old solution, I found a way to create a function and pass the context to it as a parameter, but the problem is that I don't need to call it from any other class that extends Activity. I want to do this from the same class extending the fragment.
How should I do it?
Any help would be much appreciated.
source to share
Try adding this code to your previous activity:
// a static variable to get a reference of our application context
public static Context contextOfApplication;
public static Context getContextOfApplication()
{
return contextOfApplication;
}
and in the same activity, in the onCreate method add this line:
contextOfApplication = getApplicationContext();
In your snippet, you can access this using:
Context applicationContext = YourActivity.getContextOfApplication();
applicationContext.getContentResolver();
source to share
if (requestCode == GALLERY) {
if (resultCode==RESULT_OK){
if (data!=null) {
Uri uri = data.getData();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, options);
options.inSampleSize = calculateInSampleSize(options, 100, 100);
options.inJustDecodeBounds = false;
Bitmap image = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, options);
//imageofpic.setImageBitmap(image);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else {
Toast.makeText(getActivity(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
}
source to share