如何从内部存储读取图像

时间:2018-10-30 14:14:16

标签: android

我已将图像下载到我的应用程序数据/数据/包内存中。它已下载,但问题是我没有在imageviewer中显示图像的路径。但不能。请告诉我如何显示。请注意路径。谢谢 这是我的下载代码。

ImageView imageView = (ImageView) findViewById(R.id.iv);
        String mUrl = "https://cometonice.com/im.gif";
        InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.GET, mUrl,
                new Response.Listener<byte[]>() {
                    @Override
                    public void onResponse(byte[] response) {
                        // TODO handle the response
                        try {
                            if (response != null) {

                                FileOutputStream outputStream;
                                String name = "im.gif";
                                outputStream = openFileOutput(name, Context.MODE_PRIVATE);
                                outputStream.write(response);
                                outputStream.close();
                                Toast.makeText(MainActivity.this, "Download complete.", Toast.LENGTH_LONG).show();
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO handle the error
                error.printStackTrace();
            }
        }, null);
        RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new HurlStack());
        mRequestQueue.add(request);

1 个答案:

答案 0 :(得分:0)

那么您可以像这样使用排球的ImageRequest

ImageRequest request = new ImageRequest(url,
    new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap bitmap) {
            imageView.setImageBitmap(bitmap);
            saveBitmapToFile(bitmap)
        }
    }, 0, 0, null,
    new Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {

        }
    });

并保存为:

public String saveBitmapToFile(Bitmap bitmap) {
        FileOutputStream out = null;
        String filename = null;
        try {
            File f = new File(Environment.getExternalStorageDirectory(), "myapp");
            if (!f.exists()) {
                f.mkdirs();
            }
            filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myapp/" + UUID.randomUUID().toString() + ".jpg";
            out = new FileOutputStream(filename);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filename;
    }
相关问题