如何从API端点下载图像或位图,而不使用ImageView?

huangapple go评论113阅读模式
英文:

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(&quot;url&quot;,
            response -&gt; {

            }, 0, 0, null,
            error -&gt; {

            }) {
        @Override
        public Map&lt;String, String&gt; getHeaders() {
            Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
            params.put(&quot;param1&quot;, &quot;num1&quot;);
            params.put(&quot;param2&quot;, &quot;num2&quot;);
            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(&quot;Images&quot;,MODE_PRIVATE);

    file = new File(file, System.currentTimeMillis()+&quot;_yourFileUnique&quot;+&quot;.jpg&quot;);

    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;
}

huangapple
  • 本文由 发表于 2020年7月31日 02:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63179159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定