应用程序将多个图像保存到图库

时间:2015-11-05 08:57:28

标签: android android-intent android-activity

我想在我的应用中使用Button来让用户点击并存储多张图片 点击后,图片将保存到手机图库中的文件夹中。

如何存储多个图像并在图库中指定路径?

1 个答案:

答案 0 :(得分:0)

如果您使用网络上的网址制作图片,则可以使用以下代码 -

class DownloadFile extends AsyncTask<String,Integer,Long> {
ProgressDialog mProgressDialog = new ProgressDialog(MainActivity.this);// Change Mainactivity.this with your activity name. 
String strFolderName;
@Override
protected void onPreExecute() {
    super.onPreExecute();
    mProgressDialog.setMessage("Downloading");
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setMax(100);
    mProgressDialog.setCancelable(true);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.show();
}
@Override
protected Long doInBackground(String... aurl) {
    int count;
    try {
        URL url = new URL((String) aurl[0]);
        URLConnection conexion = url.openConnection();
        conexion.connect();
        String targetFileName="Name"+".rar";//Change name and subname
        int lenghtOfFile = conexion.getContentLength();
        String PATH = Environment.getExternalStorageDirectory()+ "/"+downloadFolder+"/";
        File folder = new File(PATH);
        if(!folder.exists()){
            folder.mkdir();//If there is no folder it will be created.
        }
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(PATH+targetFileName);
        byte data[] = new byte[1024];
        long total = 0;
        while ((count = input.read(data)) != -1) {
            total += count;
                   publishProgress ((int)(total*100/lenghtOfFile));
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {}
    return null;
}
protected void onProgressUpdate(Integer... progress) {
     mProgressDialog.setProgress(progress[0]);
     if(mProgressDialog.getProgress()==mProgressDialog.getMax()){
        mProgressDialog.dismiss();
        Toast.makeText(fa, "File Downloaded", Toast.LENGTH_SHORT).show();
     }
}
protected void onPostExecute(String result) {
}

}

现在在您的清单中添加以下权限 -

     <uses-permission android:name="android.permission.INTERNET"> </uses- permission>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
参考

参考this link