相机uri在不同的Android版本中给出null?

时间:2016-09-10 14:33:21

标签: android camera fragment uri

我使用下面的代码捕获图像并将位图隐藏到base64字符串。我面临的问题是,下面的代码在棒棒糖中成功运行但是当在marshmallow中运行时它给出了uri空指针执行。我该如何解决?

.footer

以下是我的错误

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == getActivity().RESULT_OK) {

            //user is returning from capturing an image using the camera
            if (requestCode == CAMERA_CAPTURE) {
                //filePath = data.getData();

                Bundle extras = data.getExtras();
                //get the cropped bitmap
                thePic = (Bitmap) extras.get("data");

                try {
                    byte[] inputData = null;
                    // String f = compressImage(uri.toString());
                    thePic = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());


                    ByteArrayOutputStream stream = new ByteArrayOutputStream();

                    thePic.compress(Bitmap.CompressFormat.JPEG, 15, stream);

                    byte[] image = stream.toByteArray();

                    encodedString = Base64.encodeToString(image, Base64.DEFAULT);
                    //   imageView.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }


                //thePic.compress(Bitmap.CompressFormat.JPEG, 100, iStream);
                if (setInImageView.equals("1")) {

                    imageView1.setImageBitmap(thePic);
                    image1 =encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE1_SHARED_PREF, image1);
                    //Saving values to editor
                    editor2.commit();

                } else if (setInImageView.equals("2")) {

                    imageView2.setImageBitmap(thePic);
                    image2 = encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE2_SHARED_PREF, image2);
                    //Saving values to editor
                    editor2.commit();

                } else if (setInImageView.equals("3")) {

                    imageView3.setImageBitmap(thePic);
                    image3 = encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE3_SHARED_PREF, image3);
                    //Saving values to editor
                    editor2.commit();

                } else if (setInImageView.equals("4")) {

                    imageView4.setImageBitmap(thePic);
                    image4 = encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE4_SHARED_PREF, image4);
                    //Saving values to editor
                    editor2.commit();

                } else if (setInImageView.equals("5")) {

                    imageView5.setImageBitmap(thePic);
                    image5 = encodedString;

                    //Creating a shared preference
                    SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences
                            (ConfigFormData.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    editor2 = sharedPreferences2.edit();
                    editor2.putString(ConfigFormData.METERIMAGE5_SHARED_PREF, image5);
                    //Saving values to editor
                    editor2.commit();

                }

            }
        }
    }

public void selectImage1() {
        try {
            //use standard intent to capture an image
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //we will handle the returned data in onActivityResult
            startActivityForResult(captureIntent, CAMERA_CAPTURE);
        } catch (ActivityNotFoundException anfe) {
            //display an error message
            String errorMessage = "oops! your device doesn't support capturing images!";
            Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
        }
    }

0 个答案:

没有答案