通过onActivityResult设置位图:传递结果失败

时间:2016-12-06 09:31:58

标签: java android android-intent bitmap onactivityresult

我正在使用startActivityForResult和onActivityResult尝试从相机/图库中获取图像,并以编程方式生成的ImageViews显示它们。我有Gallery图像工作和显示,但是我遇到了相机图像的问题。

我在listAdapter中调用了startActivityForResult,用于gallery按钮(打开cusotm gallery类以选择多个图像)和 formListAdapter.java 中的相机按钮(打开相机应用程序):

private void openCustomGallery() {
    Intent intent = new Intent(context, CustomPhotoGalleryActivity.class);
    context.startActivityForResult(intent, PICK_IMAGE_MULTIPLE);
}

private void launchCamera() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    context.startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

然后使用switch语句调用onActivityResult以确定来自我的主要活动 formsActivity.java的请求代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case PICK_IMAGE_MULTIPLE:
                Log.i("Gallery Request Fired", "Yes");

                //Declare imagePaths / ArrayList to store bitmaps
                imagesPathList = new ArrayList<String>();
                bitmapImages = new ArrayList<Bitmap>();
                String[] imagesPath = data.getStringExtra("data").split("\\|");

                //Loop through selected images
                for (int i = 0; i < imagesPath.length; i++) {
                    //Reduce file size
                    compressBitmap = decodeSampledBitmapFromResource(imagesPath[i],
                            350, 350);
                    //Add image to array
                    bitmapImages.add(compressBitmap);
                }

                //Send bitmap to listview & refresh
                formListAdapter.getBitmap(bitmapImages);
                adapter.notifyDataSetChanged();
                break;
            case CAMERA_REQUEST:
                Log.i("Camera Request Fired", "Yes");

                //I've tried various logic here, however bitmap is always null and never set.
                Bitmap datifoto = null;
                Uri picUri = data.getData();//<- get Uri here from data intent
                if (picUri != null) {
                    try {
                        //Not setting datifoto
                        datifoto = android.provider.MediaStore.Images.Media.getBitmap(
                                this.getContentResolver(),
                                picUri);

                        //ERROR HERE ==> Null pointer exception because "datifoto" is null and not set
                        bitmapImages.add(datifoto);
                    } catch (FileNotFoundException e) {
                        throw new RuntimeException(e);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                break;
            default:
                Log.i("Default", "Yes");
                break;
        }
    }
}

然而,这始终是一个&#34>尝试调用虚方法&#39; boolean java.util.ArrayList.add(java.lang.Object)&#39;在null对象引用&#34;因为我没有设置位图,所以我试图将空对象添加到数组中:

Caused by : java.lang.RuntimeException : Failure delivering result ResultInfo {
    who = null,
    request = 1888,
    result = -1,
    data = Intent {act = inline - data dat = content : //media/external/images/media/18125 (has extras) }} 
    to activity {net.swnton_blandpjkeary.james.pjkearymobileinspections/net.swnton_blandpjkeary.james.pjkearymobileinspections.formsActivity}: 
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference   

理想情况下,我会设置位图,将其添加到ArrayList,然后将此列表传递给适配器,以便在ImageView中进行设置。我假设我没有正确处理Intent数据,但我尝试了大量具有相同结果的解决方案。

提前感谢您的任何帮助:)

1 个答案:

答案 0 :(得分:1)

我必须在switch语句之外声明bitmapImages = new ArrayList<>(); ...:|