Android : "Failure delivering result ResultInfo, NullPointerException" - managedQuery inside ArrayAdapter

on Thursday, April 16, 2015


Inside my custom ArrayAdapter I try to convert a Uri to a bitmap image. The code works fine in the parent activity but I need to move it inside the ArrayAdapter.


As I do so, I get a NullPointerException at the line:



//query the data
Cursor picCursor = ((Activity) context).managedQuery(pickedUri, medData, null, null, null);
if(picCursor!=null)


I guess it is a Context related issue, but I can't find a fix. Could someone help me out?


logcat:



04-16 21:45:01.175: E/AndroidRuntime(19929): FATAL EXCEPTION: main 04-16 21:45:01.175: E/AndroidRuntime(19929): Process: foto.studio, PID: 19929 04-16 21:45:01.175: E/AndroidRuntime(19929): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/17550 (has extras) }} to activity {foto.studio/foto.studio.MainActivity}: java.lang.NullPointerException 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.app.ActivityThread.deliverResults(ActivityThread.java:3681) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3724) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.app.ActivityThread.access$1400(ActivityThread.java:175) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.os.Handler.dispatchMessage(Handler.java:102) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.os.Looper.loop(Looper.java:146) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.app.ActivityThread.main(ActivityThread.java:5602) 04-16 21:45:01.175: E/AndroidRuntime(19929): at java.lang.reflect.Method.invokeNative(Native Method) 04-16 21:45:01.175: E/AndroidRuntime(19929): at java.lang.reflect.Method.invoke(Method.java:515) 04-16 21:45:01.175: E/AndroidRuntime(19929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 04-16 21:45:01.175: E/AndroidRuntime(19929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 04-16 21:45:01.175: E/AndroidRuntime(19929): at dalvik.system.NativeStart.main(Native Method) 04-16 21:45:01.175: E/AndroidRuntime(19929): Caused by: java.lang.NullPointerException 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.content.ContentResolver.query(ContentResolver.java:448) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.content.ContentResolver.query(ContentResolver.java:407) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.app.Activity.managedQuery(Activity.java:1898) 04-16 21:45:01.175: E/AndroidRuntime(19929): at foto.studio.PhotosListViewAdapter.pathToBitmap(PhotosListViewAdapter.java:189) 04-16 21:45:01.175: E/AndroidRuntime(19929): at foto.studio.PhotosListViewAdapter.getView(PhotosListViewAdapter.java:72) 04-16 21:45:01.175: E/AndroidRuntime(19929): at foto.studio.PhotosFragmentActivity.addToImageList(PhotosFragmentActivity.java:115) 04-16 21:45:01.175: E/AndroidRuntime(19929): at foto.studio.PhotosFragmentActivity.onActivityResult(PhotosFragmentActivity.java:158) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.app.Activity.dispatchActivityResult(Activity.java:5643) 04-16 21:45:01.175: E/AndroidRuntime(19929): at android.app.ActivityThread.deliverResults(ActivityThread.java:3677) 04-16 21:45:01.175: E/AndroidRuntime(19929): ... 11 more



Custom ArrayAdapter:



public class PhotosListViewAdapter extends ArrayAdapter<ImageItemsSetter> {

DeleteImageListener dListener;
private Context context;
private ArrayList<ImageItemsSetter> data = new ArrayList<ImageItemsSetter>();
boolean scrolled = false;
private ImageView Img;
private EditText quantity;
private Spinner spinner;
private Button delete;


//standard constructor
public PhotosListViewAdapter(Context context, ArrayList<ImageItemsSetter> data) {
super(context, android.R.id.content, data);
this.context = context;
this.data = data;
}



@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.photos_list, null);

ImageItemsSetter image = data.get(position);


Img = (ImageView) view.findViewById(R.id.Img);



//HERE I AM CALLING THE PATH TO BITMAP METHOD
Img.setImageBitmap(pathToBitmap(image.getTheImageUri()));

quantity.setText(image.getQuantity());
System.out.println("recorder image format: " + image.getFormat());



return view;
}


//Interface to send selected image's position for deletion
public void setDeleteImageListener(DeleteImageListener listener) {
this.dListener = listener;
}

public static interface DeleteImageListener {
public void onDeletePressed(int position);
public void onQuantityChanged(String quantity, String name);
public void onFormatChanged(String format, String name);
}

//METHOD THAT CONVERT THE Uri to Bitmap
public Bitmap pathToBitmap(Uri pickedUri){

//declare the bitmap
Bitmap pic = null;

//declare the path string
String imgPath = "";

//retrieve the string using media data
String[] medData = { MediaStore.Images.Media.DATA };
System.out.println("medData is: " + medData);

//PROBLEM HERE:
Cursor picCursor = ((Activity) context).managedQuery(pickedUri, medData, null, null, null);
if(picCursor!=null)
{
//get the path string
int index = picCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
picCursor.moveToFirst();
imgPath = picCursor.getString(index);
}
else
imgPath = pickedUri.getPath();

//if we have a new URI attempt to decode the image bitmap
if(pickedUri!=null) {

//set the width and height we want to use as maximum display
int targetWidth = 600;
int targetHeight = 400;

//create bitmap options to calculate and use sample size
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();

//first decode image dimensions only - not the image bitmap itself
bmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgPath, bmpOptions);

//image width and height before sampling
int currHeight = bmpOptions.outHeight;
int currWidth = bmpOptions.outWidth;

//variable to store new sample size
int sampleSize = 1;

//calculate the sample size if the existing size is larger than target size
if (currHeight>targetHeight || currWidth>targetWidth)
{
//use either width or height
if (currWidth>currHeight)
sampleSize = Math.round((float)currHeight/(float)targetHeight);
else
sampleSize = Math.round((float)currWidth/(float)targetWidth);
}

//use the new sample size
bmpOptions.inSampleSize = sampleSize;

//now decode the bitmap using sample options
bmpOptions.inJustDecodeBounds = false;

//get the file as a bitmap
pic = BitmapFactory.decodeFile(imgPath, bmpOptions);



}
return pic;

}

}

0 comments:

Post a Comment