gpt4 book ai didi

android - 如何从 sdcard 中选择一个图像文件,然后如何将它发送到服务器?

转载 作者:行者123 更新时间:2023-11-30 04:24:53 24 4
gpt4 key购买 nike


//下面

在我的应用程序中,我有一个按钮从 SD 卡中选择图像,单击它后,我想浏览所有图像,然后用户将选择任何图像,之后所选图像文件将上传到服务器.

而且我还有一个按钮 from camera 单击它,首先我想拍摄快照,然后上传到服务器,请通过示例告诉我任何方式?

最佳答案

这是我用来查询手机的 MediaStore 并返回包含所有图像的游标对象的代码。之后您可以将它们上传到您的服务器,但我建议您在 AsyncTask 中处理这第一部分。

class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {

//Load images from SDCARD and display
@Override
protected Object doInBackground(Object... params) {
//setProgressBarIndeterminateVisibility(true);
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null;

// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Thumbnails._ID};
// Create the cursor pointing to the SDCard
Cursor cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0) {
//No Images available, post some message to the user
}
int imageID = 0;
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
imageID = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
}
} catch (IOException e) {
//Error fetching image, try to recover
}
}
cursor.close();
return null;
}

关于android - 如何从 sdcard 中选择一个图像文件,然后如何将它发送到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8676681/

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