Select an image. Foreboding raises nullpointer exception

public void uploadpicture(View view) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == SELECT_PICTURE) { Uri selectedImageUri = data.getData(); selectedImagePath = getPath(selectedImageUri); Toast.makeText(this,selectedImagePath,Toast.LENGTH_SHORT).show(); Log.i("selectedImagePath",selectedImagePath.trim()); } } } 

I got this from Log-cat:

 05-24 15:57:51.054: E/AndroidRuntime(18016): FATAL EXCEPTION: main 05-24 15:57:51.054: E/AndroidRuntime(18016): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.sec.gallery3d.provider/picasa/item/5147009501910019474 }} to activity {ncpl.talentapp/ncpl.talentapp.SignUp}: java.lang.NullPointerException 05-24 15:57:51.054: E/AndroidRuntime(18016): at android.app.ActivityThread.deliverResults(ActivityThread.java:2821) 05-24 15:57:51.054: E/AndroidRuntime(18016): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2864) 05-24 15:57:51.054: E/AndroidRuntime(18016): at android.app.ActivityThread.access$1000(ActivityThread.java:122) 05-24 15:57:51.054: E/AndroidRuntime(18016): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1057) 05-24 15:57:51.054: E/AndroidRuntime(18016): at android.os.Handler.dispatchMessage(Handler.java:99) 05-24 15:57:51.054: E/AndroidRuntime(18016): at android.os.Looper.loop(Looper.java:132) 05-24 15:57:51.054: E/AndroidRuntime(18016): at android.app.ActivityThread.main(ActivityThread.java:4126) 05-24 15:57:51.054: E/AndroidRuntime(18016): at java.lang.reflect.Method.invokeNative(Native Method) 05-24 15:57:51.054: E/AndroidRuntime(18016): at java.lang.reflect.Method.invoke(Method.java:491) 05-24 15:57:51.054: E/AndroidRuntime(18016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 05-24 15:57:51.054: E/AndroidRuntime(18016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 05-24 15:57:51.054: E/AndroidRuntime(18016): at dalvik.system.NativeStart.main(Native Method) 05-24 15:57:51.054: E/AndroidRuntime(18016): Caused by: java.lang.NullPointerException 
+4
source share
3 answers

From your log, it looks like you are trying to select an image from the Picasa folder dat=content://com.android.sec.gallery3d.provider/picasa/item/5147009501910019474 , and they should be processed differently because the data in the column MediaStore.Images.Media.DATA will be empty for them. You can see how to properly process Picasa images at this link:

http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/

Only you need to handle URIs that start with "content: //com.sec.android.gallery3d" (I see this on Samsung S3), so change

 if (selectedImage.toString().startsWith("content://com.google.android.gallery3d")) 

to

 if (_selectedImage.toString().startsWith("content://com.google.android.gallery3d") || selectedImage.toString().startsWith("content://com.sec.android.gallery3d")) 

I am not sure if there are any other special cases (maybe yes).

+9
source

I ran into the same problem and tried to apply it on user1682516. I hit my head against the wall because my Picasa content URLs didn’t exactly match: my looked like "com.sec.android.gallery3d.provider" and not "com.android.gallery3d.provider", but it seems to work whether I changed the url or not.

Then I felt stupid when I realized that the library that I use to upload remote images for ImageViews in my application, https://github.com/nostra13/Android-Universal-Image-Loader , takes care of this for me. I can just call ImageLoader.getInstance (). LoadImage () with the appropriate parameters and get the bitmap in the callback. I don’t have to worry about whether it is a local image, a deleted Picasa image, or a regular remote image β€” that took care of me. I wish I thought about it a few hours ago!

+5
source

Verification code here

 public class SelectPhotoActivity extends Activity { private static final int SELECT_PICTURE = 1; private String selectedImagePath=""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivityForResult(intent, SELECT_PICTURE); } public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == SELECT_PICTURE) { Uri selectedImageUri = data.getData(); selectedImagePath = getPath(selectedImageUri); // here you can set the image } } } } 
-2
source

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


All Articles