java - 如何在不使用 ImageView 的情况下从 API 端点下载图像或位图?
问题描述
我正在尝试使用 android(java) 中的Volley或okhttp3从端点下载图像,有一些方法可以在中显示该图像,有没有一种方法可以从 API 端点下载图像,并将其保存在文件夹中子文件夹名为. 我已经有一段时间了,有点像 Volley 和 okhttp 的初学者。如果有人可以帮助我,那就太好了。ImageView
assets
images
到目前为止,我已经尝试使用 ImageRequest,
public void getImages(String url) {
RequestQueue mRequestQueue = Volley.newRequestQueue(this);
ImageRequest request = new ImageRequest("url",
response -> {
}, 0, 0, null,
error -> {
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<String, String>();
params.put("param1", "num1");
params.put("param2", "num2");
return params;
}
};
mRequestQueue.add(request);
}
解决方案
如评论中所述。无法修改资产文件夹。考虑将其保存在本地存储中
protected Uri saveImageToInternalStorage(Bitmap bitmap){
ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
File file = wrapper.getDir("Images",MODE_PRIVATE);
file = new File(file, System.currentTimeMillis()+"_yourFileUnique"+".jpg");
try{
OutputStream stream = null;
stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
}catch (IOException e) // Catch the exception
{
e.printStackTrace();
}
Uri savedImageURI = Uri.parse(file.getAbsolutePath());
return savedImageURI;
}
推荐阅读
- java - Google App Engine 无法启动 Dataflow 作业
- c++ - 使用 [] 访问字符串元素会打印随机结果
- r - 计算 r 中每个患者的持续时间
- javascript - 如何使用来自 Ajax JQuery 的响应更新 html 元素
- python - 使用pandas DataFrame 比较两个file_csv 找出差异并将F2 加入F1
- php - 函数内array_push(mainArr,subAssociativeArr)的PHP问题
- powerbi - 计算累计总计时如何提取月份
- python - 如何从 django 中的预先编写的视图中删除 put
- format - 首次使用时,MIFARE Ultralight 上的 OTP 页面是否应该为空?
- html - 如何删除活动li选项卡上的边框