Я сталкиваюсь с проблемой с моим кодом, когда пытаюсь получить изображение с помощью средства выбора файлов, проблема возникает в «onActivityResult»: при попытке Uri soundImagePath = data.getData (); я могу получить данные, но тогда растровое изображение остается нулевым, как это:
if (requestCode == PICK_IMAGE_REQUEST){
if (resultCode==RESULT_OK){
soundImagePath = data.getData();
try {
Bitmap bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),soundImagePath);
//ImageDecoder imageDecoder = ImageDecoder.decodeBitmap(ImageDecoder.createSource(requireContext()., soundImagePath))
imageChoose.setImageBitmap(bm);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
вторая вещь, которую я попробовал (работал с другим фрагментом)
//option 2:
//verifyStoragePermissions(getActivity());
/*soundImagePath = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
if (soundImagePath != null) {
Cursor cursor = getActivity().getContentResolver().query(soundImagePath,
filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
imageChoose.setImageBitmap(BitmapFactory.decodeFile(picturePath));
handleImageUpload(BitmapFactory.decodeFile(picturePath));
cursor.close();
}
}*/
//System.out.println("mage"+soundImagePath.toString());
}
}
}
private void verifyStoragePermissions(FragmentActivity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
ошибка, которую я получаю из обоих вариантов :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.musicwithnav, PID: 11459
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65770, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:66202 flg=0x1 }} to activity {com.example.musicwithnav/com.example.musicwithnav.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at android.app.ActivityThread.deliverResults(Unknown Source:111)
at android.app.ActivityThread.handleSendResult(Unknown Source:156)
at android.app.servertransaction.ActivityResultItem.execute(Unknown Source:11)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(Unknown Source:77)
at android.app.servertransaction.TransactionExecutor.execute(Unknown Source:73)
at android.app.ActivityThread$H.handleMessage(Unknown Source:43)
at android.os.Handler.dispatchMessage(Unknown Source:19)
at android.os.Looper.loop(Unknown Source:249)
at android.app.ActivityThread.main(Unknown Source:98)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
at com.android.internal.os.ZygoteInit.main(Unknown Source:282)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at com.example.musicwithnav.ui.addFiles.AddFragment.onActivityResult(AddFragment.java:206)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:170)
at com.example.musicwithnav.MainActivity.onActivityResult(MainActivity.java:42)
at android.app.Activity.dispatchActivityResult(Unknown Source:11)
at android.app.ActivityThread.deliverResults(Unknown Source:44)
очень большое спасибо за помощь!