如何在不使用ImageView的情况下从API端点下载图像或位图?

时间:2020-07-30 18:24:53

标签: java android android-studio android-volley okhttp

我正在尝试使用android(java)中的 Volley okhttp3 从端点下载图像,有多种方法可以显示该图像ImageView中的图像,有没有一种方法可以从API端点下载图像,并保存在名为assets的子文件夹的images文件夹中。我已经有一段时间了,有点像Volley和okhttp的初学者。如果有人可以帮助我,那就太好了。

到目前为止,我已经尝试使用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 :(得分: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) // Catch the exception
    {
        e.printStackTrace();
    }
    Uri savedImageURI = Uri.parse(file.getAbsolutePath());
    return savedImageURI;
}