Android画廊照片 - 阅读字节以供上传
我正在尝试让用户通过画廊选择图像。 我的目标是将所选图像字节字节上传到WebService。不幸的是,我仅设法在ImageView中显示映像,而不是从文件中读取字节。
意图:
val galleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galleryIntent, Constants.INTENT_ID_GALLERY)
on结果:
val imageUri: Uri? = data!!.data
将image设置为imageView(works):
imageview1!!.setImageURI(imageUri)
读取文件字节:(不起作用)
val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
val cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null)
cursor!!.moveToFirst()
val columnIndex = cursor!!.getColumnIndex(filePathColumn[0])
var picturePath = cursor!!.getString(columnIndex)
cursor!!.close()
val file = File(picturePath)
val bytes = org.apache.commons.io.FileUtils.readFileToByteArray(file)
在Picture Path中,我得到的文件类似:
/storage/3234-3064/DCIM/Camera/file.jpg
/storage/3234-3064/DCIM/Screenshots/file.jpg
/storage/emulated/0/Downloads/file.jpg
/storage/emulated/0/Pictures/file.jpg
/storage/emulated/0/Autodesk/SketchBook3/SketchBookPreview/file.jpg
请任何人告诉我如何正确访问文件并阅读其字节 。
在Crydle中,我添加了:实现“ Commons-io:commons-io:2.7”
I'm trying to let the user select an image via gallery. My goal is to upload the selected images bytes to a webservice. Unfortunately, I only manage to display the image in an ImageView, but not to read the bytes from the file.
Intent:
val galleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galleryIntent, Constants.INTENT_ID_GALLERY)
On Result:
val imageUri: Uri? = data!!.data
Set Image to ImageView (works):
imageview1!!.setImageURI(imageUri)
Reading file bytes: (does not work)
val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
val cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null)
cursor!!.moveToFirst()
val columnIndex = cursor!!.getColumnIndex(filePathColumn[0])
var picturePath = cursor!!.getString(columnIndex)
cursor!!.close()
val file = File(picturePath)
val bytes = org.apache.commons.io.FileUtils.readFileToByteArray(file)
In picturePath I get files like:
/storage/3234-3064/DCIM/Camera/file.jpg
/storage/3234-3064/DCIM/Screenshots/file.jpg
/storage/emulated/0/Downloads/file.jpg
/storage/emulated/0/Pictures/file.jpg
/storage/emulated/0/Autodesk/SketchBook3/SketchBookPreview/file.jpg
Please anybody tell me how to properly access the File and read its bytes.
In crydle I added: implementation "commons-io:commons-io:2.7"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在java2s上找到了一个代码片段:
android contentResolver read readbytes(uri uri,contentResolver resolver)
效果很好,可以获得URI的字节,最后:-)
I found a code snippet on java2s:
Android ContentResolver Read readBytes(Uri uri, ContentResolver resolver)
Works just fine to get the bytes of the Uri, finally :-)