当前位置:   article > 正文

uniapp 安卓 plus调用原生文件选择_uniapp 调取原生文件选择

uniapp 调取原生文件选择
async PickFile(callback, acceptType) {
				try {
					await this.getPermissions()
					var CODE_REQUEST = 1000;
					var main = plus.android.runtimeMainActivity();
					if (plus.os.name == 'Android' && typeof callback == 'function') {
						var Intent = plus.android.importClass('android.content.Intent');
						var intent = new Intent(Intent.ACTION_GET_CONTENT);
						intent.addCategory(Intent.CATEGORY_OPENABLE);
						if (acceptType) {
							intent.setType(acceptType);
						} else {
							intent.setType("*/*");
						}
						main.onActivityResult = function(requestCode, resultCode, data) {
							if (requestCode == CODE_REQUEST) {
								var uri = data.getData();
								plus.android.importClass(uri);
								var Build = plus.android.importClass('android.os.Build');
								var isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

								var DocumentsContract = plus.android.importClass(
									'android.provider.DocumentsContract');
								// DocumentProvider
								if (isKitKat && DocumentsContract.isDocumentUri(main, uri)) {
									console.log("版本大于 4.4 ", uri.getAuthority());
									// ExternalStorageProvider
									if ("com.android.externalstorage.documents" == uri.getAuthority()) {
										var docId = DocumentsContract.getDocumentId(uri);
										var split = docId.split(":");
										var type = split[0];
										console.log(type)
										if ("primary" == type) {
											var Environment = plus.android.importClass('android.os.Environment');
											callback(Environment.getExternalStorageDirectory() + "/" + split[1]);
										} else {
											var System = plus.android.importClass('java.lang.System');
											var sdPath = System.getenv("SECONDARY_STORAGE");
											if (sdPath) {
												callback(sdPath + "/" + split[1]);
											}
										}
									}
									// DownloadsProvider
									else if ("com.android.providers.downloads.documents" == uri.getAuthority()) {
										// uni.showToast({
										// 	title: '当前模块文件不可用!请切换为其他模块选择文件',
										// 	icon: 'none'
										// })
										// return false
										// 文件选择是下载内容 非根目录时 导致文件路径出问题 目前没有解决办法禁用次模块
										
										var id = DocumentsContract.getDocumentId(uri);
										var ContentUris = plus.android.importClass('android.content.ContentUris');
										console.log(id, '+++++++++++++++++++++', uri.getScheme())
										if (id.indexOf('msf:') > -1) {
											id = id.split(":")[1]
										}
										console.log(id, '+++++++++++++++++++++')
										let Uri = plus.android.importClass('android.net.Uri')
										// console.log(Uri.parse("content://downloads/public_downloads"))
										var contentUri = ContentUris.withAppendedId(
											// Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
											Uri.parse("content://downloads/public_downloads"), id);
										console.log(contentUri)

										if (contentUri) {
											let path = getDataColumn(main, contentUri, null, null)
											callback(path);
											return false
										}
										contentUri = ContentUris.withAppendedId(
											// Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
											Uri.parse("content://downloads/my_downloads"), id);
										console.log(contentUri)

										if (contentUri) {
											let path = getDataColumn(main, contentUri, null, null)
											callback(path);
											return false
										}
										let fileName = getFileName(main, uri)
										console.log(fileName,'--------------------')
										callback(getFileExplorer('/storage/emulated/0/Download',fileName+''))
										return "fileName = " + fileName;
									}
									// MediaProvider
									else if ("com.android.providers.media.documents" == uri.getAuthority()) {
										console.log(uri)
										var docId = DocumentsContract.getDocumentId(uri);
										var split = docId.split(":");
										var type = split[0];
										console.log(type)
										var MediaStore = plus.android.importClass('android.provider.MediaStore');
										let contentUri = ""
										if ("image" == type) {
											contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
										} else if ("video" == type) {
											contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
										} else if ("audio" == type) {
											contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
										} else {
											contentUri = MediaStore.Files.getContentUri("external")
										}
										var selection = "_id=?";
										var selectionArgs = new Array();
										selectionArgs[0] = split[1];
										callback(getDataColumn(main, contentUri, selection, selectionArgs));
									}
								}
								// MediaStore (and general)
								else if ("content" == uri.getScheme()) {
									callback(getDataColumn(main, uri, null, null));
								}
								// File
								else if ("file" == uri.getScheme()) {
									callback(uri.getPath());
								}
							}
						}
						main.startActivityForResult(intent, CODE_REQUEST);
					}
				} catch (e) {
					console.log(e)
				}


				function getFileExplorer(path, fileName) {
					try {
						let File = plus.android.importClass("java.io.File")
						let directory = new File(path);
						let files = directory.listFiles()
						let nowPath = ''
						for(let i =0;i<files.length;i++){
							
							if (files[i].isDirectory()) {
								nowPath = getFileExplorer(files[i].getAbsolutePath(),fileName)
							}else{
								console.log(files[i].getAbsolutePath(),'/storage/emulated/0/Download/WeiXin/最高额信用借款合同补充协议.pdf'.indexOf(fileName),typeof fileName)
								if(files[i].getAbsolutePath().indexOf(fileName) > -1) nowPath = files[i].getAbsolutePath()
							}
							if(nowPath){
								break
							}
						}
						return nowPath
					} catch (e) {
						//TODO handle the exception
						console.log(e)
					}
				}

				function getDataColumn(main, uri, selection, selectionArgs) {
					plus.android.importClass(main.getContentResolver());
					let cursor = main.getContentResolver().query(uri, ['_data'], selection, selectionArgs,
						null);
					console.log(cursor)
					plus.android.importClass(cursor);
					if (cursor != null && cursor.moveToFirst()) {
						var column_index = cursor.getColumnIndexOrThrow('_data');
						var result = cursor.getString(column_index)
						cursor.close();
						return result;
					}
					return null;
				}

				function getFileName(context, uri) {
					console.log('', context.getContentResolver())
					plus.android.importClass(context.getContentResolver());
					try {
						let mimeType = context.getContentResolver().getType(uri);
						console.log(mimeType)
						let filename = null;

						if (mimeType == null && context != null) {
							/* String path = getPath(context, uri);
							 if (path == null) {
							     filename = getName(uri.toString());
							 } else {
							     File file = new File(path);
							     filename = file.getName();
							 }*/
						} else {
							let returnCursor = context.getContentResolver().query(uri, null,
								null, null, null);
							console.log(returnCursor)
							if (returnCursor != null) {
								plus.android.importClass(returnCursor)
								let OpenableColumns = plus.android.importClass('android.provider.OpenableColumns')
								let nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);

								console.log("FUCKFUCK nameIndex", "" + nameIndex);
								returnCursor.moveToFirst();
								console.log(returnCursor.getString(2))
								filename = returnCursor.getString(nameIndex);
								returnCursor.close();
							}
						}
						return filename;
					} catch (e) {
						console.log(e)
					}

				}

				function getDocumentCacheDir(context) {
					try {
						let File = plus.android.importClass('java.io.File')
						let dir = new File('/storage/emulated/0/Download/', 'documents');
						if (!dir.exists()) {
							dir.mkdirs();
						}
						return dir;
					} catch (e) {
						console.log(e)
					}

				}

				function generateFileName(name, directory) {
					if (name == null) {
						return null;
					}
					let File = plus.android.importClass('java.io.File')
					let file = new File(directory, name);

					if (file.exists()) {
						let fileName = name;
						let extension = "";
						let dotIndex = name.lastIndexOf('.');
						if (dotIndex > 0) {
							fileName = name.substring(0, dotIndex);
							extension = name.substring(dotIndex);
						}

						let index = 0;

						while (file.exists()) {
							index++;
							name = fileName + '(' + index + ')' + extension;
							file = new File(directory, name);
						}
					}

					try {
						if (!file.createNewFile()) {
							return null;
						}
					} catch (e) {
						console.log(e)
						return null;
					}

					return file;
				}

				function saveFileFromUri(context, uri, destinationPath) {
					plus.android.importClass('java.io.InputStream');
					let is = null;
					let bos = null;
					let bis = null;
					let FileOutputStream = plus.android.importClass('java.io.FileOutputStream')
					let BufferedOutputStream = plus.android.importClass('java.io.BufferedOutputStream')
					let BufferedInputStream = plus.android.importClass('java.io.BufferedInputStream')
					let FileInputStream = plus.android.importClass('java.io.FileInputStream')
					try {
						plus.android.importClass(context.getContentResolver());

						is = context.getContentResolver().openInputStream(uri)
						bos = new BufferedOutputStream(new FileOutputStream(destinationPath));
						let buffer = new Int8Array(1024);
						bos.write(is.read(buffer));
					} catch (e) {
						console.log(e)
					} finally {
						try {
							if (is != null) is.close();
							if (bos != null) bos.close();
						} catch (e) {
							console.log(e)
						}
					}
				}
			}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285

调用方法

this.PickFile((e) => {
  //文件回调
						console.log(e, '=================')
						//文件类型
					}, 'application/*|text/*')
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/298521
推荐阅读
相关标签
  

闽ICP备14008679号