Unable to receive camera image with intent to select 4.2.2 AVD

I am working on a part of my application that allows the user to select an image either from the camera or from the gallery using the choice of intent.

It works fine on my 2.2.1 android phone, but when I compile it on 4.2.2 AVD, it returns a null pointer error when I use the camera,

public void onClick(View View) { Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null); galleryIntent.setType("image/*"); galleryIntent.setAction(Intent.ACTION_GET_CONTENT); galleryIntent.addCategory(Intent.CATEGORY_OPENABLE); Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); Intent chooser = new Intent(Intent.ACTION_CHOOSER); chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent); chooser.putExtra(Intent.EXTRA_TITLE, "Chooser"); Intent[] intentArray = {cameraIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); startActivityForResult(chooser,REQUEST_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) { try { if (bitmap != null) { bitmap.recycle(); } InputStream stream = getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(stream); stream.close(); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } super.onActivityResult(requestCode, resultCode, data); } } 

this is the error i get:
05-05 05: 03: 31.730: E / AndroidRuntime (820): caused by: java.lang.NullPointerException

And he said that the problem is in this line:

 InputStream stream = getContentResolver().openInputStream(data.getData()); 

What am I doing wrong?

Updated:

Now it works and here is the solution:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) { if(data.getData()!=null) { try { if (bitmap != null) { bitmap.recycle(); } InputStream stream = getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(stream); stream.close(); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { bitmap=(Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(bitmap); } super.onActivityResult(requestCode, resultCode, data); } } 

Thank you for your help!

+4
source share
1 answer

Hi, try this code.

The following code is designed to work with the camera button:

 imgviewCamera.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //define the file-name to save photo taken by Camera activity String fileName = "new-photo-name.jpg"; //create parameters for Intent with filename ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, fileName); values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera"); //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState) imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); //create new Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, PICK_Camera_IMAGE); } }); OnActivityresult code will be like below protected void onActivityResult(int requestCode, int resultCode, Intent data) { Uri selectedImageUri = null; String filePath = null; switch (requestCode) { case PICK_Camera_IMAGE: if (resultCode == RESULT_OK) { //use imageUri here to access the image selectedImageUri = imageUri; } else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show(); } break; } if(selectedImageUri != null){ try { // OI FILE Manager String filemanagerstring = selectedImageUri.getPath(); // MEDIA GALLERY String selectedImagePath = getPath(selectedImageUri); if (selectedImagePath != null) { filePath = selectedImagePath; } else if (filemanagerstring != null) { filePath = filemanagerstring; } else { Toast.makeText(getApplicationContext(), "Unknown path", Toast.LENGTH_LONG).show(); Log.e("Bitmap", "Unknown path"); } if (filePath != null) { Toast.makeText(getApplicationContext(), " path" + filePath, Toast.LENGTH_LONG).show(); Intent i = new Intent(getApplicationContext(), EditorActivity.class); // passing array index i.putExtra("id", filePath); startActivity(i); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Internal error", Toast.LENGTH_LONG).show(); Log.e(e.getClass().getName(), e.getMessage(), e); } } } public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); if (cursor != null) { // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } else return null; } 

Remember to add the camera resolution to the manifest file.
Hope this helps you.

+4
source

Source: https://habr.com/ru/post/1479210/


All Articles