从相册获取图片并转换为文件数组并上传
程序从一个dialog开始: private void show() { final Dialog dialog = new Dialog(this, R.style.ActionSheetDialogStyle); View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_item, null); //相册 Button bt_camera = inflate.findViewById(R.id.bt_camera); //取消 Button bt_cancel = inflate.findViewById(R.id.bt_cancel); dialog.setContentView(inflate); Window dialogWindow = dialog.getWindow(); dialogWindow.setGravity(Gravity.BOTTOM); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); lp.y = 20; dialogWindow.setAttributes(lp); //取消按钮 bt_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); //从相册选取 bt_camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); if (ContextCompat.checkSelfPermission(FeedbackDetails.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(FeedbackDetails.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } else { openAlbum(); } } }); dialog.show(); } private void openAlbum() { Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO);//打开相册 } //相册 @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { openAlbum(); } else { Toast.makeText(this, "你拒绝了权限申请,可能无法打开相册哟", Toast.LENGTH_SHORT).show(); } break; default: } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case CHOOSE_PHOTO: if (resultCode == RESULT_OK) { // 判断手机系统版本号 if (Build.VERSION.SDK_INT >= 19) { // 4.4及以上系统使用这个方法处理图片 handleImageOnKitKat(data); } else { // 4.4以下系统使用这个方法处理图片 handleImageBeforeKitKat(data); } } break; default: break; } } @TargetApi(19) private void handleImageOnKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(this, uri)) { // 如果是document类型的Uri,则通过document id处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; // 解析出数字格式的id String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri. getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content: downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { // 如果是content类型的Uri,则使用普通方式处理 imagePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { // 如果是file类型的Uri,直接获取图片路径即可 imagePath = uri.getPath(); } displayImage(imagePath); // 根据图片路径显示图片 } private void handleImageBeforeKitKat(Intent data) { Uri uri = data.getData(); String imagePath = getImagePath(uri, null); displayImage(imagePath); } private String getImagePath(Uri uri, String selection) { String path = null; // 通过Uri和selection来获取真实的图片路径 Cursor cursor = getContentResolver().query(uri, null, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { path = cursor.getString(cursor.getColumnIndex(MediaStore. Images.Media.DATA)); } cursor.close(); } return path; } //获取图片地址 private void displayImage(String imagePath) { if (imagePath != null) { Bitmap bitmap = BitmapFactory.decodeFile(imagePath); headFile = saveMyBitmap(bitmap, "head"); setsend("", headFile); } else { Toast.makeText(this, "获取图片失败", Toast.LENGTH_SHORT).show(); } } //将bitmap转化为png格式 public File saveMyBitmap(Bitmap mBitmap, String prefix) { File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File file = null; try { file = File.createTempFile( prefix, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); FileOutputStream fos = new FileOutputStream(file); mBitmap.compress(Bitmap.CompressFormat.JPEG, 10, fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } return file; }