英文:
How do I download an image or bitmap from an API endpoint without using ImageView?
问题
I am trying to download an image from an endpoint using Volley or okhttp3 in android(java), there are ways to show that image in ImageView
, is there a way I could download an image from an API endpoint, and save it in assets
folder in a sub-folder named images
. I have been at it for a while now, kind of a beginner with Volley and okhttp. It would be great if someone could help me out.
So far I have tried using 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);
}
英文:
I am trying to download an image from an endpoint using Volley or okhttp3 in android(java), there are ways to show that image in ImageView
, is there a way I could download an image from an API endpoint, and save it in assets
folder in a sub-folder named images
. I have been at it for a while now, kind of a beginner with Volley and okhttp. It would be great if someone could help me out.
So far I have tried using 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);
}
答案1
得分: 0
如评论中所述,不可能修改资产文件夹,请考虑将其保存在本地存储中。
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) {
e.printStackTrace();
}
Uri savedImageURI = Uri.parse(file.getAbsolutePath());
return savedImageURI;
}
英文:
As said in the comments .it is not possible to modify the assets folder . consider saving it in local storage
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论