如何在Android上下载图像并将其保存到图库

发布于 2024-12-05 03:09:39 字数 59 浏览 2 评论 0原文

我从服务器获取图像链接,单击该链接应该下载并将其保存到图库中,我不知道如何执行此操作。请帮助我,提前致谢

I get image link from the server and on click of the link it should download and save it to the gallery, I have no clue on how to do it. please help me on this, thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燕归巢 2024-12-12 03:09:39
**combine the below code to store image at a particula path**

File storagePath = new File(Environment
                    .getExternalStorageDirectory()
                    + "/com.logistics.herestethiparty/images/");
            storagePath.mkdirs();
            File file = new File(storagePath, fileImageName);    

**i have used the below code in asyncclass for image download**

    URL url = new URL(imageUrl);
                    HttpURLConnection urlConnection = (HttpURLConnection) url
                            .openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);
                    urlConnection.connect();
                    FileOutputStream fileOutput = new FileOutputStream(file);
                    InputStream inputStream = urlConnection.getInputStream();
                    int downloadedSize = 0;

                    byte[] buffer = new byte[1024];
                    int bufferLength = 0;
                    while ((bufferLength = inputStream.read(buffer)) > 0) {
                        fileOutput.write(buffer, 0, bufferLength);
                        downloadedSize += bufferLength;
                    }
                    // close the output stream when done
                    fileOutput.close();
**combine the below code to store image at a particula path**

File storagePath = new File(Environment
                    .getExternalStorageDirectory()
                    + "/com.logistics.herestethiparty/images/");
            storagePath.mkdirs();
            File file = new File(storagePath, fileImageName);    

**i have used the below code in asyncclass for image download**

    URL url = new URL(imageUrl);
                    HttpURLConnection urlConnection = (HttpURLConnection) url
                            .openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);
                    urlConnection.connect();
                    FileOutputStream fileOutput = new FileOutputStream(file);
                    InputStream inputStream = urlConnection.getInputStream();
                    int downloadedSize = 0;

                    byte[] buffer = new byte[1024];
                    int bufferLength = 0;
                    while ((bufferLength = inputStream.read(buffer)) > 0) {
                        fileOutput.write(buffer, 0, bufferLength);
                        downloadedSize += bufferLength;
                    }
                    // close the output stream when done
                    fileOutput.close();
我的黑色迷你裙 2024-12-12 03:09:39

试试这段代码,调用mDownloadAndSave()方法将图像保存到SDCard上,它将解决您的问题。

public void mDownloadAndSave() {
    // Setting up file to write the image to.
    File f = new File("/mnt/sdcard/img.png");

    // Open InputStream to download the image.
    InputStream is;
    try {
        is = new URL("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg").openStream();

        // Set up OutputStream to write data into image file.
        OutputStream os = new FileOutputStream(f);

        CopyStream(is, os);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
            break;
            os.write(bytes, 0, count);
        }
    } catch (Exception ex) {

    }
}

并且不要忘记将以下权限添加到 Androidmanifest.xml 中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Try this code, Call mDownloadAndSave() method for save image on SDCard, it will solve your problem.

public void mDownloadAndSave() {
    // Setting up file to write the image to.
    File f = new File("/mnt/sdcard/img.png");

    // Open InputStream to download the image.
    InputStream is;
    try {
        is = new URL("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg").openStream();

        // Set up OutputStream to write data into image file.
        OutputStream os = new FileOutputStream(f);

        CopyStream(is, os);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
            break;
            os.write(bytes, 0, count);
        }
    } catch (Exception ex) {

    }
}

And Don't forget to add below permission into Androidmanifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文