Мне нужно, чтобы при нажатии на ImageView можно было загрузить картинку из галереи в этот ImageView. При тестировании на телефоне при открытии картинки перед ее помещением в ImageView возникает ошибка
public class EditProfile extends AppCompatActivity { private final int PICK_IMAGE_REQUEST = 1; private static final int STORAGE_PERMISSION_CODE = 911; private String imagePath; ImageView ivEditProfil; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit_profile); ivEditProfil = (ImageView) findViewById(R.id.iv_edit_profil); requestStoragePermission(); ivEditProfil.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showFileChooser(); } }); } private void showFileChooser() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_PICK); startActivityForResult(Intent.createChooser(intent, "Выберите изображение"), PICK_IMAGE_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { Uri selectedImageUri = data.getData(); imagePath = getRealPathFromURI(selectedImageUri); Picasso.with(this) .load(new File(imagePath)) .resize(200, 200) .centerCrop() .into(ivEditProfil); } } private String getRealPathFromURI(Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; android.support.v4.content.CursorLoader loader = new android.support.v4.content.CursorLoader(this, contentUri, proj, null, null, null); Cursor cursor = loader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String result = cursor.getString(column_index); cursor.close(); return result; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == STORAGE_PERMISSION_CODE) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "Предоставлены права на чтение.", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Права на чтение не были предоставлены.", Toast.LENGTH_LONG).show(); } } } //Requesting permission private void requestStoragePermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) return; if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { //If the user has denied the permission previously your code will come to this block //Here you can explain why you need this permission //Explain here why you need this permission } ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE); } }
Ошибка:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=file:///storage/emulated/0/download/21-54-59-photo.jpg typ=image/jpeg flg=0x1 }} to activity {com.halfway.yogamylife/com.halfway.yogamylife.EditProfile}: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:3744) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3787) at android.app.ActivityThread.access$ 1500(ActivityThread.java:153) at android.app.ActivityThread$ H.handleMessage(ActivityThread.java:1424) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:5529) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$ MethodAndArgsCaller.run(ZygoteInit.java:739) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference at com.halfway.yogamylife.EditProfile.getRealPathFromURI(EditProfile.java:241) at com.halfway.yogamylife.EditProfile.onActivityResult(EditProfile.java:227) at android.app.Activity.dispatchActivityResult(Activity.java:6508) at android.app.ActivityThread.deliverResults(ActivityThread.java:3740) ... 9 more