0

I'm calling image intent method to get the options of camera or gallary while clicking on image view.I found the code here Allow user to select camera or gallery for image user David Manpearl

and this is the code I'm using

public static final int YOUR_SELECT_PICTURE_REQUEST_CODE=222;

 public void openImageIntent1() {

        // Determine Uri of camera image to save.
        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "userDir" + File.separator);
        root.mkdirs();
        final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);

            // Camera.
            final List<Intent> cameraIntents = new ArrayList<Intent>();
            final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            final PackageManager packageManager = this.getPackageManager();
            final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
            for(ResolveInfo res : listCam) {
                final String packageName = res.activityInfo.packageName;
                final Intent intent = new Intent(captureIntent);
                intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                cameraIntents.add(intent);
            }

            // Filesystem.
            final Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

            // Chooser of filesystem options.
            final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

            // Add the camera options.
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));

            startActivityForResult(chooserIntent, 222);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode == RESULT_OK) {
                if(requestCode == 222) {
                    final boolean isCamera;
                    if(data == null)
                        isCamera = true;
                    else {
                        final String action = data.getAction();
                        if(action == null)
                            isCamera = false;
                        else
                            isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    }
                    Uri selectedImageUri;
                    if(isCamera)
                        selectedImageUri = outputFileUri;
                    else
                        selectedImageUri = data == null ? null : data.getData();
                }
            }
        }
    }
}

In this ,getting error on final PackageManager packageManager = this.getPackageManager() line.

Can you please help? Thank you

Community
  • 1
  • 1
cryptic
  • 168
  • 1
  • 10
  • Please post the stacktrace and how you're calling `openImageIntent1()` e.g. how you initialize the object you call the method on. – laalto Jan 20 '15 at 10:47
  • I'm calling this method in another class by making object of this call – cryptic Jan 20 '15 at 10:57

1 Answers1

1

I'm calling this method in another class by making object of this call

Never instantiate an Activity with new. It won't get properly initialized, and calling any activity or context method won't work. So it's no good for anything you'd want to use it for.

Instead, pass in a Context as a parameter to methods that need it like this.

To instantiate an activity, use startActivity() with Intent.

laalto
  • 150,114
  • 66
  • 286
  • 303