leijie 5 年之前
父節點
當前提交
12bf0996f6

+ 6 - 4
display-client/assets/mobile-action.xml

@ -72,11 +72,11 @@
72 72
	<action name="browserFile" class="com.ailk.mobile.client.func.SwitchActivity" method = "browserFile"/>
73 73
	<action name="openDisplayPage" class="com.ailk.mobile.client.func.SwitchActivity" method = "openDisplayPage"/>
74 74
	<!-- MobileMap -->
75
	<!--  
75
 
76 76
	<action name="location" class="com.ai.ipu.map.func.MobileMap" method="location"></action>
77
	 <action name="markMap" class="com.ai.ipu.map.func.MobileMap" method="markMap"></action>
78
	 <action name="selectLocation" class="com.ai.ipu.map.func.MobileMap" method="selectLocation"></action> 
79
	 -->
77
	<action name="markMap" class="com.ai.ipu.map.func.MobileMap" method="markMap"></action>
78
	<action name="selectLocation" class="com.ai.ipu.map.func.MobileMap" method="selectLocation"></action> 
79
	 
80 80
	<!-- MobileStorage -->
81 81
	<action name="removeMemoryCache" class="com.ai.ipu.mobile.plugin.MobileStorage" method="removeMemoryCache"></action>
82 82
	<action name="clearMemoryCache" class="com.ai.ipu.mobile.plugin.MobileStorage" method="clearMemoryCache"></action>
@ -196,4 +196,6 @@
196 196
	<action name="cleanKeyDownFlag" class="com.ai.ipu.mobile.plugin.MobileUI" method="cleanKeyDownFlag"></action>
197 197
	
198 198
	<action name="openIPUFromOtherApp" class="com.ai.ipu.func.OpenOtherApp" method="openIPUFromOtherApp"></action>
199
	
200
	<action name="downloadFile"  class="com.ai.ipu.func.DownloadFile" method="downloadFile"/>
199 201
</actions>

+ 119 - 0
display-client/src/com/ai/ipu/func/DownloadFile.java

@ -0,0 +1,119 @@
1
package com.ai.ipu.func;
2
3
import android.app.Activity;
4
import android.content.Intent;
5
import android.net.Uri;
6
import android.os.AsyncTask;
7
import android.os.Environment;
8
import android.text.TextUtils;
9
import android.widget.Toast;
10
11
12
import org.json.JSONArray;
13
14
import com.ai.ipu.mobile.frame.IIpuMobile;
15
import com.ai.ipu.mobile.frame.plugin.Plugin;
16
17
import java.io.BufferedInputStream;
18
import java.io.File;
19
import java.io.FileOutputStream;
20
import java.io.InputStream;
21
import java.net.URL;
22
import java.net.URLConnection;
23
24
/**
25
 * 下载图片与视频
26
 * Created by larryjay on 18/5/22.
27
 */
28
29
public class DownloadFile extends Plugin {
30
	private boolean isLoading = false;
31
	String relDirPath = "/car";
32
	
33
    public DownloadFile(IIpuMobile wademobile) {
34
        super(wademobile);
35
    }
36
37
    /**
38
     * @param params 参数1 文件资源类型(供IOS使用)  参数2 文件资源地址 参数3(可选) 文件的相对路径
39
     * 成功为0,失败为1
40
     */
41
    public void downloadFile(JSONArray params){
42
    	if(!isLoading){    		
43
    		String downloadUrl = params.optString(1);
44
    		relDirPath = TextUtils.isEmpty(params.optString(2)) ? relDirPath : params.optString(2);
45
    		String localDirPath = Environment.getExternalStorageDirectory().toString() + relDirPath;
46
    		downloadFileAsync(context, downloadUrl, localDirPath);
47
    	}
48
    }
49
50
    private void downloadFileAsync(final Activity context, final String downloadUrl, final String localDirPath){
51
        new AsyncTask<String,Integer,Boolean>(){
52
53
            @Override
54
            protected Boolean doInBackground(String... strings) {
55
                try {
56
                	isLoading = true;
57
                    downloadFile(context,downloadUrl,localDirPath);
58
                } catch (Exception e) {
59
                	isLoading = false;
60
                    e.printStackTrace();
61
                    toast(context,"下载异常,保存失败!");
62
                    callback(1 + "");
63
                }
64
                callback(0 + "");
65
                isLoading = false;
66
                return true;
67
            }
68
        }.execute();
69
    }
70
71
    private void downloadFile(final Activity context, String downloadUrl, String localDirPath) throws Exception{
72
        File fileDir = new File(localDirPath);
73
        if(!fileDir.exists()){
74
            fileDir.mkdirs();
75
        }
76
        String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1);
77
        String filePath = localDirPath + "/" + fileName;
78
        File file = new File(filePath);
79
        if(file.exists()){
80
            toast(context,"文件已存在");
81
            return;
82
        }
83
        InputStream in = getInByUrlConn(downloadUrl);
84
        FileOutputStream fos = new FileOutputStream(file);
85
        byte[] data = new byte[1024];
86
        int count;
87
        while((count = in.read(data)) != -1){
88
            fos.write(data,0,count);
89
        }
90
        fos.flush();
91
        fos.close();
92
        in.close();
93
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
94
        Uri uri = Uri.fromFile(file);
95
        intent.setData(uri);
96
        context.sendBroadcast(intent);
97
        toast(context,"下载成功,文件已保存到" + filePath);
98
    }
99
100
    /**
101
     * @param downloadUrl
102
     * @return
103
     * @throws Exception
104
     */
105
    private InputStream getInByUrlConn(String downloadUrl) throws Exception{
106
        URL url = new URL(downloadUrl);
107
        URLConnection conn = url.openConnection();
108
        return new BufferedInputStream(conn.getInputStream());
109
    }
110
111
    private void toast(final Activity context, final String msg){
112
        context.runOnUiThread(new Runnable() {
113
            @Override
114
            public void run() {
115
                Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
116
            }
117
        });
118
    }
119
}

+ 1 - 1
ipu-plugin-basic/build.gradle

@ -15,7 +15,7 @@ dependencies {
15 15
	compileOnly group: 'junit', name: 'junit', version:'4.12'
16 16
	compileOnly group: 'android', name: 'android', version:'22'
17 17
	compileOnly group: 'com.ai.wade', name: 'wade-mobile-data', version:'1.0'
18
    compileOnly "android:android-lite-http:1.0"
18
	compileOnly "android:android-lite-http:1.0"
19 19
	compileOnly "com.ai.ipu:ipu-basic:${IPU_VERSION}"
20 20
	compileOnly "com.ai.ipu.mobile:ipu-mobile-basic:${IPU_VERSION}"
21 21
	compileOnly "com.ai.ipu.mobile:ipu-mobile-framework:${IPU_VERSION}"