带有进度条的Cordova / Phonegap原生文件下载插件

时间:2014-01-12 09:56:07

标签: java javascript android cordova

正在制作Android Phonegap应用程序,下载mp3文件并将其存储在Device的SD卡上的Music目录中。

我偶然发现了一个处理文件下载的原生android cordova插件。它甚至还有一个进度条。

我的问题是。完成文件下载后的插件总是将文件存储在手机的内部存储器中,而不是存储在手机的SD卡上的音乐目录中。

如果有人可以修改插件以将下载的文件直接保存到音乐目录中,我将不胜感激。

我将代码发布在下面插件的Download.java文件中。

package com.cmpsoft.mobile.plugin.download;

   import java.io.File;
   import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import java.net.HttpURLConnection;
   import java.net.MalformedURLException;
   import java.net.URL;

   import org.apache.cordova.CallbackContext;
   import org.apache.cordova.CordovaPlugin;
   import org.json.JSONArray;
   import org.json.JSONException;
   import android.annotation.SuppressLint;
   import android.app.AlertDialog;
   import android.app.Dialog;
   import android.app.AlertDialog.Builder;
   import android.content.Context;
   import android.content.DialogInterface;
   import android.content.Intent;
   import android.net.Uri;
   import android.os.Environment;
   import android.os.Handler;
   import android.os.Message;
   import android.view.Gravity;
   import android.view.ViewGroup.LayoutParams;
   import android.widget.LinearLayout;
   import android.widget.ProgressBar;
   import android.widget.TextView;
    import android.widget.Toast;

    @SuppressLint({ "DefaultLocale", "HandlerLeak" })
    public class DownLoad extends CordovaPlugin {

/ / The following layout parameters
   / / Identify the current situation of the control width and height FILL_PARENT =   occupy all parent controls, WRAP_CONTENT = only the contents of the package control / /   There are other effects such as left and right margins, where we use the default

@SuppressWarnings("deprecation")
private LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
private LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

private String fileName;
private String path;
private boolean openFlag;

/ * Download * /
private static final int DOWNLOAD = 1;
/ * Download end * /
private static final int DOWNLOAD_FINISH = 2;
/ * Download save path * /
private String mSavePath;
/ * Record progress bar number * /
private int progress;
/ * Whether to cancel the update * /
private boolean cancelUpdate ;

private Context mContext;
/ * Update progress bar * /
private ProgressBar mProgress;
private Dialog mDownloadDialog;
private TextView textView;
/ * Open the file types * /
private final String[][] type_amp = {
        { ".3gp", "video/3gpp" },
        { ".apk", "application/vnd.android.package-archive" },
        { ".asf", "video/x-ms-asf" },
        { ".avi", "video/x-msvideo" },
        { ".bin", "application/octet-stream" },
        { ".bmp", "image/bmp" },
        { ".c", "text/plain" },
        { ".class", "application/octet-stream" },
        { ".conf", "text/plain" },
        { ".cpp", "text/plain" },
        { ".doc", "application/msword" },
        { ".docx",
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
        { ".xls", "application/vnd.ms-excel" },
        { ".xlsx",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
        { ".exe", "application/octet-stream" },
        { ".gif", "image/gif" },
        { ".gtar", "application/x-gtar" },
        { ".gz", "application/x-gzip" },
        { ".h", "text/plain" },
        { ".htm", "text/html" },
        { ".html", "text/html" },
        { ".jar", "application/java-archive" },
        { ".java", "text/plain" },
        { ".jpeg", "image/jpeg" },
        { ".jpg", "image/jpeg" },
        { ".js", "application/x-javascript" },
        { ".log", "text/plain" },
        { ".m3u", "audio/x-mpegurl" },
        { ".m4a", "audio/mp4a-latm" },
        { ".m4b", "audio/mp4a-latm" },
        { ".m4p", "audio/mp4a-latm" },
        { ".m4u", "video/vnd.mpegurl" },
        { ".m4v", "video/x-m4v" },
        { ".mov", "video/quicktime" },
        { ".mp2", "audio/x-mpeg" },
        { ".mp3", "audio/x-mpeg" },
        { ".mp4", "video/mp4" },
        { ".mpc", "application/vnd.mpohun.certificate" },
        { ".mpe", "video/mpeg" },
        { ".mpeg", "video/mpeg" },
        { ".mpg", "video/mpeg" },
        { ".mpg4", "video/mp4" },
        { ".mpga", "audio/mpeg" },
        { ".msg", "application/vnd.ms-outlook" },
        { ".ogg", "audio/ogg" },
        { ".pdf", "application/pdf" },
        { ".png", "image/png" },
        { ".pps", "application/vnd.ms-powerpoint" },
        { ".ppt", "application/vnd.ms-powerpoint" },
        { ".pptx",
                "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
        { ".prop", "text/plain" }, { ".rc", "text/plain" },
        { ".rmvb", "audio/x-pn-realaudio" }, { ".rtf", "application/rtf" },
        { ".sh", "text/plain" }, { ".tar", "application/x-tar" },
        { ".tgz", "application/x-compressed" }, { ".txt", "text/plain" },
        { ".wav", "audio/x-wav" }, { ".wma", "audio/x-ms-wma" },
        { ".wmv", "audio/x-ms-wmv" },
        { ".wps", "application/vnd.ms-works" }, { ".xml", "text/plain" },
        { ".z", "application/x-compress" },
        { ".zip", "application/x-zip-compressed" }, { "", "*/*" } };

@Override
public boolean execute(String action, JSONArray args,
        CallbackContext callbackContext) throws JSONException {
    init(args.getString(0),args.getString(1),args.getBoolean(2));
    showDownloadDialog();
    return super.execute(action, args, callbackContext);
}

private void init(String path,String fileName,boolean openFlag){
    this.mContext = this.cordova.getActivity();
    this.path = path;
    this.fileName = fileName;
    this.openFlag = openFlag;
    this.cancelUpdate = false;
}

/ **
   * Display software download dialog
  * /
private void showDownloadDialog() {
    AlertDialog.Builder builder = new Builder(mContext);
    builder.setTitle("Downloading Music");
    / / Add a linear layout
    LinearLayout layout = new LinearLayout(mContext);
    layout.setLayoutParams(LP_FW);
    / / Set the child controls to center
    layout.setGravity(Gravity.CENTER);
    layout.setOrientation(LinearLayout.VERTICAL); 
            / / Controls on its way to the  vertical, horizontal default
            / / Create a progress bar
    mProgress = new ProgressBar(mContext, null,
            android.R.attr.progressBarStyleHorizontal);
    mProgress.setLayoutParams(LP_FW);
    layout.addView(mProgress);
    // 创建百分比现实textview
    textView = new TextView(mContext);
    textView.setLayoutParams(LP_WW);
    layout.addView(textView);
    builder.setView(layout);
    / / Cancel the update
    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                                        / / Set the cancel state "̬
                    cancelUpdate = true;
                }
            });
    mDownloadDialog = builder.create();
    mDownloadDialog.show();
    / / Download file
    downloadFile();
}

/ **
   * According to the document type to get open
   *
   * @ Param file
   * @ Return
   * /
@SuppressLint("DefaultLocale")
private String getOpenType(File file) {
    String type = "*/*";
    String fName = file.getName();
    / / Get the extension delimiter before "." FName in position.
    int dotIndex = fName.lastIndexOf(".");
    if (dotIndex > 0) {
        return type;
    }
    / * Get the file extension * /
    String end = fName.substring(dotIndex, fName.length()).toLowerCase();
    if (end == "")
        return type;
    / / Find the corresponding file type MIME type matching table.
    for (int i = 0; i < type_amp.length; i++) {
        if (end.equals(type_amp[i][0]))
            type = type_amp[i][1];
    }
    return type;
}

/ **
   * Open the file
    * /
private void openFile() {
    File file  = new File(mSavePath,fileName);
    if (!file.exists()) {
        return;
    }
    / / Install APK files via Intent
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setDataAndType(Uri.parse("file://" + file.toString()),
            getOpenType(file));
    try {
        mContext.startActivity(i);
    } catch (Exception e) {
        Toast.makeText(mContext, "Unable To Open" + file.getName(),
                Toast.LENGTH_SHORT).show();
    }

};

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        / / Downloading
        case DOWNLOAD:
            / / Downloading...
            mProgress.setProgress(progress);
            / / Set the percentage
            textView.setText(progress + "%");
            break;
        case DOWNLOAD_FINISH:
            / / Setup file
            if(openFlag){
                openFile();
            }
            break;
        default:
            break;
        }
    }

};

/ **
   * Download file
   * /
private void downloadFile() {
    / / Start a new thread to download file
    new DownloadFileThread().start();

}
class DownloadFileThread extends Thread {
    @Override
    public void run() {
        try {
/ / Determine whether there is an SD card, and whether the     read and write permissions
                URL url = new URL(path);
                / / Create connection
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                conn.connect();
                / / Get the file size
                int length = conn.getContentLength();
                / / Create an input stream
                InputStream is = conn.getInputStream();

                FileOutputStream fos = getOutStream(fileName);
                int count = 0;
                / / Cache
                byte buf[] = new byte[1024];
                / / Write to a file
                do {
                    int numread = is.read(buf);
                    count += numread;
        / / Calculate the position of the progress bar
                    progress = (int) (((float) count / length) * 100);
                    / / Update progress
                    mHandler.sendEmptyMessage(DOWNLOAD);
                    if (numread <= 0) {
                        / / Download is complete
                        mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
                        break;
                    }
                    / / Write to file
                    fos.write(buf, 0, numread);
                } while (!cancelUpdate);/ / Click Cancel to stop  downloading.
                fos.close();
                is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        / / Cancel the download dialog box displays
        mDownloadDialog.dismiss();
    }

}
/**
    * @param fileName
    * @return
    * @throws FileNotFoundException
   */
@SuppressWarnings("deprecation")
@SuppressLint("WorldReadableFiles")
private FileOutputStream getOutStream(String fileName) throws FileNotFoundException{
    if (Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED)) {
        String sdpath = Environment.getExternalStorageDirectory()
                + "/";
        mSavePath = sdpath + "download";
        File file = new File(mSavePath);
        / / Determine the file directory exists
        if (!file.exists()) {
            file.mkdir();
        }
        File saveFile = new File(mSavePath, fileName);
        return new FileOutputStream(saveFile);
    }else{
        mSavePath = mContext.getFilesDir().getPath();/ / Get the system directory to store files /data/data/<package name>/files  
        return mContext.openFileOutput(fileName , Context.MODE_WORLD_READABLE);
    }
}

}

1 个答案:

答案 0 :(得分:0)

快速简便的解决方法是更换:

String sdpath = Environment.getExternalStorageDirectory()
            + "/";

String sdpath = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_MUSIC)
            + "/";

但是,我想更新此插件,以便您可以指定应用程序内部存储或外部存储。如果是外部存储设备,您应该选择音乐,图片,电影,播客或下载。

如果Github上的源代码在某处让我知道。