gpt4 book ai didi

Android - 从图库中选择图像并将其存储在文件类型变量中

转载 作者:太空狗 更新时间:2023-10-29 16:38:11 24 4
gpt4 key购买 nike

我是 Android 开发新手。我想从 Android 设备的图库中选择图片或视频。将其存储在 File 类型的变量中。我正在这样做,因为我需要使用我的应用程序中的 Android API 将图像/视频上传到 dropbox。构造函数接受 File 类型的第四个参数。我不确定要将什么作为文件传递,因为我搜索的所有示例都显示了使用 url 并制作位图在 ImageView 中选择的图像。

imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

这是代码,我有。

final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
//to get image and videos, I used a */"
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, 1);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
imagepath = getPath(selectedImageUri);
}
}

public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
yourSelectedImage = BitmapFactory.decodeFile(filePath);
return cursor.getString(column_index);
}

最佳答案

您只需创建一个File 变量,其中包含您从图库中选择的图像路径。将您的 OnActivityResult 更改为:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
imagepath = getPath(selectedImageUri);
File imageFile = new File(imagepath);
}
}

关于Android - 从图库中选择图像并将其存储在文件类型变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com