"传递结果失败ResultInfo,NullPointerException" - ArrayAdapter中的managedQuery

时间:2015-04-16 18:57:48

标签: android bitmap nullpointerexception cursor uri

在我的自定义ArrayAdapter中,我尝试将Uri转换为位图图像。代码在父活动中正常工作,但我需要在ArrayAdapter中移动它。

当我这样做时,我在行:

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

我猜这是一个与Context有关的问题,即使我传递了父活动的上下文,我也得到了同样的错误。有人可以帮助我吗?

logcat的:

  

04-16 21:45:01.175:E / AndroidRuntime(19929):致命异常:主要   04-16 21:45:01.175:E / AndroidRuntime(19929):处理:foto.studio,   PID:19929 04-16 21:45:01.175:E / AndroidRuntime(19929):   java.lang.RuntimeException:传递结果失败   ResultInfo {who = null,request = 65537,result = -1,data = Intent {   dat = content:// media / external / images / media / 17550(有额外内容)}}到   活动{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):引起: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更多

自定义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;

        }

    }

1 个答案:

答案 0 :(得分:0)

不幸的是,我在获取Uri时遇到的查询在我的SQLite数据库中是错误的。所以我的&#34; pickUri&#34;是空的。我已经修复了查询,一切都顺利了。