Android 使用毕加索下载并保存图像

Android 使用毕加索下载并保存图像,android,image,storage,picasso,Android,Image,Storage,Picasso,我想下载和保存多个图像与毕加索图书馆,但我没有找到如何保存在外部存储。。。(SD) 有可能吗?据我所知这是不可能的。毕加索只在单独的线程中异步缓存和下载图像 我建议使用Piccaso缓存和显示图片,并使用异步任务下载图像并将其保存到外部存储 class DownloadFileFromURL extends AsyncTask<String, String, String> { /** * Before starting background thread

我想下载和保存多个图像与毕加索图书馆,但我没有找到如何保存在外部存储。。。(SD)


有可能吗?据我所知这是不可能的。毕加索只在单独的线程中异步缓存和下载图像

我建议使用Piccaso缓存和显示图片,并使用异步任务下载图像并将其保存到外部存储

   class DownloadFileFromURL extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.println("Starting download");
    }

    /**
     * Downloading file in background thread
     * */
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            String root = Environment.getExternalStorageDirectory().toString();

            System.out.println("Downloading");
            URL url = new URL(f_url[0]);

            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
            int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file

            OutputStream output = new FileOutputStream(root+"/downloadedfile.jpg");
            byte data[] = new byte[1024];

            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;

                // writing data to file
                output.write(data, 0, count);

            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }



    /**
     * After completing background task
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        System.out.println("Downloaded");
    }

}
class DownloadFileFromURL扩展异步任务{
/**
*在启动后台线程之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
System.out.println(“开始下载”);
}
/**
*在后台线程中下载文件
* */
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
String root=Environment.getExternalStorageDirectory().toString();
System.out.println(“下载”);
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
//获取文件长度
int lenghtOfFile=conconnect.getContentLength();
//读取文件的输入流-带8k缓冲区
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
//输出流以写入文件
OutputStream output=新文件OutputStream(root+“/downloaddedfile.jpg”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//将数据写入文件
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
/**
*完成后台任务后
* **/
@凌驾
受保护的void onPostExecute(字符串文件\u url){
System.out.println(“下载”);
}
}
您只需使用

    new DownloadFileFromURL().execute(<The url you want to download from>);
新建下载文件fromURL().execute();
您还需要向清单文件添加以下权限

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

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


希望有帮助

你能给我举个例子吗?我刚刚开始学习Android,我认为目标课程是可能的,但是我在文档中找不到一些东西。这个问题已经得到了回答。
  <uses-permission android:name="android.permission.INTERNET" />

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