Intent i = new Intent(Intent.ACTION_GET_CONTENT);i.addCategory(Intent.CATEGORY_OPENABLE);i.setType("image/*");i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);*画廊*.launch(i);gallery 基本上不推荐使用 startActivityForResult(i,123) 和 OnActivityResult 方法,gallery 是替代方法,定义如下ActivityResultLauncher画廊 = selectPhotoFromGallery();和choosePhotoFromGallery()是下面定义的方法private ActivityResultLauncher选择照片从画廊(){返回 registerForActivityResult(新的 ActivityResultContracts.StartActivityForResult(),结果 ->{尝试 {如果(result.getResultCode()== RESULT_OK){如果(空!= result.getData()){如果 (result.getData().getClipData() != null) {ClipData mClipData = result.getData().getClipData();for (int i = 0; i < mClipData.getItemCount(); i++) {ClipData.Item item = mClipData.getItemAt(i);uri uri = item.getUri();String imageFilePathColumn = getPathFromURI(this, uri);productImagesList.add(imageFilePathColumn);}} 别的 {如果 (result.getData().getData() != null) {Uri mImageUri = result.getData().getData();String imageFilePathColumn = getPathFromURI(this, mImageUri);productImagesList.add(imageFilePathColumn);}}} 别的 {showToast(this, "你还没有选择图片");productImagesList.clear();}} 别的 {productImagesList.clear();}} 捕获(异常 e){e.printStackTrace();showToast(this, 出了点问题");productImagesList.clear();}});}和getPathFromURI()是下面定义的方法 public String getPathFromURI(Context context, Uri contentUri) {输出流输出;文件文件 = getPath();尝试 {如果 (file.createNewFile()) {InputStream iStream = 上下文 != null ?context.getContentResolver().openInputStream(contentUri) : context.getContentResolver().openInputStream(contentUri);byte[] inputData = getBytes(iStream);out = new FileOutputStream(file);out.write(inputData);关闭();返回 file.getAbsolutePath();}} catch (IOException e) {e.printStackTrace();}返回空;}private byte[] getBytes(InputStream inputStream) 抛出 IOException {ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();int bufferSize = 1024;字节[]缓冲区=新字节[缓冲区大小];int len = 0;while ((len = inputStream.read(buffer)) != -1) {byteBuffer.write(buffer, 0, len);}返回 byteBuffer.toByteArray();}getPath() 是私有文件 getPath() {文件夹 = new File(Environment.getExternalStorageDirectory(), 下载");如果 (!folder.exists()) {文件夹.mkdir();}返回新文件(文件夹.getPath(),System.currentTimeMillis()+.jpg");}提前致谢 编码愉快class com.bumptech.glide.load.engine.GlideException: Received null model 解决方案 Sample Preview Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); *gallery*.launch(i);ActivityResultLauncher<Intent> gallery = choosePhotoFromGallery();and choosePhotoFromGallery() is method which is define belowprivate ActivityResultLauncher<Intent> choosePhotoFromGallery() { return registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { try { if (result.getResultCode() == RESULT_OK) { if (null != result.getData()) { if (result.getData().getClipData() != null) { ClipData mClipData = result.getData().getClipData(); for (int i = 0; i < mClipData.getItemCount(); i++) { ClipData.Item item = mClipData.getItemAt(i); Uri uri = item.getUri(); String imageFilePathColumn = getPathFromURI(this, uri); productImagesList.add(imageFilePathColumn); } } else { if (result.getData().getData() != null) { Uri mImageUri = result.getData().getData(); String imageFilePathColumn = getPathFromURI(this, mImageUri); productImagesList.add(imageFilePathColumn); } } } else { showToast(this, "You haven't picked Image"); productImagesList.clear(); } } else { productImagesList.clear(); } } catch (Exception e) { e.printStackTrace(); showToast(this, "Something went wrong"); productImagesList.clear(); } });}and getPathFromURI() is method which define below public String getPathFromURI(Context context, Uri contentUri) { OutputStream out; File file = getPath(); try { if (file.createNewFile()) { InputStream iStream = context != null ? context.getContentResolver().openInputStream(contentUri) : context.getContentResolver().openInputStream(contentUri); byte[] inputData = getBytes(iStream); out = new FileOutputStream(file); out.write(inputData); out.close(); return file.getAbsolutePath(); } } catch (IOException e) { e.printStackTrace(); } return null;}private byte[] getBytes(InputStream inputStream) throws IOException { ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } return byteBuffer.toByteArray();}and the getPath() isprivate File getPath() { File folder = new File(Environment.getExternalStorageDirectory(), "Download"); if (!folder.exists()) { folder.mkdir(); } return new File(folder.getPath(), System.currentTimeMillis() + ".jpg");}THANK YOU IN ADVANCE HAPPY CODING 这篇关于当我从最近选择图像时,图像在 android 中损坏 |最近的多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-27 13:18