@Override
  public void onDownloadStart(
      final String url,
      final String userAgent,
      final String contentDisposition,
      final String mimetype,
      long contentLength) {
    String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype);
    DialogInterface.OnClickListener dialogClickListener =
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            switch (which) {
              case DialogInterface.BUTTON_POSITIVE:
                DownloadHandler.onDownloadStart(
                    mActivity, url, userAgent, contentDisposition, mimetype, false);
                break;

              case DialogInterface.BUTTON_NEGATIVE:
                break;
            }
          }
        };

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); // dialog
    builder
        .setTitle(fileName)
        .setMessage(mActivity.getResources().getString(R.string.dialog_download))
        .setPositiveButton(
            mActivity.getResources().getString(R.string.action_download), dialogClickListener)
        .setNegativeButton(
            mActivity.getResources().getString(R.string.action_cancel), dialogClickListener)
        .show();
    Log.i(Constants.TAG, "Downloading" + fileName);
  }
Ejemplo n.º 2
0
  private String getMediafilename(FeedMedia media) {
    String filename;
    String titleBaseFilename = "";

    // Try to generate the filename by the item title
    if (media.getItem() != null && media.getItem().getTitle() != null) {
      String title = media.getItem().getTitle();
      // Delete reserved characters
      titleBaseFilename = title.replaceAll("[\\\\/%\\?\\*:|<>\"\\p{Cntrl}]", "");
      titleBaseFilename = titleBaseFilename.trim();
    }

    String URLBaseFilename =
        URLUtil.guessFileName(media.getDownload_url(), null, media.getMime_type());

    if (!titleBaseFilename.equals("")) {
      // Append extension
      final int FILENAME_MAX_LENGTH = 220;
      if (titleBaseFilename.length() > FILENAME_MAX_LENGTH) {
        titleBaseFilename = titleBaseFilename.substring(0, FILENAME_MAX_LENGTH);
      }
      filename =
          titleBaseFilename
              + FilenameUtils.EXTENSION_SEPARATOR
              + FilenameUtils.getExtension(URLBaseFilename);
    } else {
      // Fall back on URL file name
      filename = URLBaseFilename;
    }
    return filename;
  }
  /**
   * Decide the file name of the final download. The file extension is derived from the MIME type.
   *
   * @param url The full URL to the content that should be downloaded.
   * @param mimeType The MIME type of the content reported by the server.
   * @param contentDisposition Content-Disposition HTTP header, if present.
   * @return The best guess of the file name for the downloaded object.
   */
  @VisibleForTesting
  public static String fileName(String url, String mimeType, String contentDisposition) {
    // URLUtil#guessFileName will prefer the MIME type extension over
    // the file extension only if the latter is of a known MIME type.
    // Therefore for things like "file.php" with Content-Type PDF, it will
    // still generate file names like "file.php" instead of "file.pdf".
    // If that's the case, rebuild the file extension from the MIME type.
    String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);
    int dotIndex = fileName.lastIndexOf('.');
    if (mimeType != null
        && !mimeType.isEmpty()
        && dotIndex > 1 // at least one char before the '.'
        && dotIndex < fileName.length()) { // '.' should not be the last char
      MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();

      String fileRoot = fileName.substring(0, dotIndex);
      String fileExtension = fileName.substring(dotIndex + 1);
      String fileExtensionMimeType = mimeTypeMap.getMimeTypeFromExtension(fileExtension);

      // If the file extension's official MIME type and {@code mimeType}
      // are the same, simply use the file extension.
      // If not, extension derived from {@code mimeType} is preferred.
      if (mimeType.equals(fileExtensionMimeType)) {
        fileName = fileRoot + "." + fileExtension;
      } else {
        String mimeExtension = mimeTypeMap.getExtensionFromMimeType(mimeType);

        if (mimeExtension != null && !mimeExtension.equals(fileExtension)) {
          fileName = fileRoot + "." + mimeExtension;
        }
      }
    }
    return fileName;
  }
Ejemplo n.º 4
0
 public static String getFileNameFromURL(String url) {
   String fileNameWithExtension = null;
   String fileNameWithoutExtension = null;
   if (URLUtil.isValidUrl(url)) {
     fileNameWithExtension = URLUtil.guessFileName(url, null, null);
     if (fileNameWithExtension != null && !fileNameWithExtension.isEmpty()) {
       String[] f = fileNameWithExtension.split(".");
       if (f != null & f.length > 1) {
         fileNameWithoutExtension = f[0];
       }
     }
   }
   return fileNameWithExtension;
 }
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
      ContentValues audioCat = AudioCatListActivity.this.audioCat.get(position);
      holder.txtCatName.setText(audioCat.getAsString("catName"));

      final File destination =
          new File(
              SmartUtils.getAnoopamMissionImageStorage()
                  + File.separator
                  + URLUtil.guessFileName(audioCat.getAsString("catImage"), null, null));
      final Uri downloadUri = Uri.parse(audioCat.getAsString("catImage").replaceAll(" ", "%20"));

      DataDownloadUtil.downloadImageFromServerAndRender(
          downloadUri, destination, holder.imgAudioCat);
    }