如何在 Android Studio 中使用 Volley 下载内部存储中的文件?

Ran*_*mar 5 java android android-studio android-volley

在我的 android 应用程序中,我想使用 android studio 中的下载管理器将文件从网络服务器下载到移动设备的内部存储。而且我还需要在 Volley 函数中使用它。

我的代码

button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
  DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  Uri uri = Uri.parse("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png");
  DownloadManager.Request request = new DownloadManager.Request(uri);
 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);                
  request.setDestinationInExternalFilesDir(DownloadsActivity.this, Environment.DIRECTORY_DOWNLOADS, "banu.jpg");
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我在按钮点击中尝试过。

Ria*_*iad 0

要从 URL 下载,您需要使用该AsyncTask过程,然后使用位图保存图像。

your class...ACTIVITY...{
 oncreateFunction(){
  // your codes...
  button.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {

     Uri uri = Uri.parse("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png");
     mTask = new DownloadTask()
                    .execute(new URL(uri));
     }
    });
}

private class DownloadTask extends AsyncTask<URL,Void,Bitmap>{

    protected void onPreExecute(){

        // if need to show progress
    }

    // Background Task/non-UI thread
    protected Bitmap doInBackground(URL...urls){
        URL url = urls[0];
        HttpURLConnection conn= null;

        try{

            conn = (HttpURLConnection) url.openConnection();
            conn.connect();

            InputStream inputStream = conn.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);

            return bmp;

        }catch(IOException e){
            e.printStackTrace();
        }finally{

            conn.disconnect();
        }
        return null;
    }

    protected void onPostExecute(Bitmap result){

        if(result!=null){

            mImageView.setImageBitmap(result);
            Uri imageInternalUri = saveImageToInternalStorage(result);              
            mImageViewInternal.setImageURI(imageInternalUri);
        }else {

            // show error if any
        }
    }
 }

 protected Uri saveImageToInternalStorage(Bitmap bitmap){

    ContextWrapper wrapper = new ContextWrapper(getApplicationContext());

    File file = wrapper.getDir("Images",MODE_PRIVATE);

    file = new File(file, "yourFileUnique"+".jpg");

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

 }
Run Code Online (Sandbox Code Playgroud)