該例實現(xiàn)的是后端返回的base64的字符串扇住,里面包含圖片和文字數(shù)據(jù),保存為pdf到手機內(nèi)存。
1、添加權限
<!-- 在cd卡中創(chuàng)建和刪除的權限·-->
<uses-permission android:name="permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 讀外存-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--用于寫入緩存數(shù)據(jù)到擴展存儲卡娘摔,需經(jīng)動態(tài)申請-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2件舵、在application中添加可分區(qū)存儲的屬性
android:requestLegacyExternalStorage="true"
3秀姐、動態(tài)申請寫入數(shù)據(jù)到存儲卡的權限
/**
* 檢查是否享有寫入外存的權限
*/
private void StartCheckWriteExternalPermission() {
if (PermissionChecker.checkSelfPermission(JSBridge.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(JSBridge.getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, SAVE_PDF_EXTER_PERMISSION);
} else {//已經(jīng)有權限
startDowloadPdf();//開始下載
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == SAVE_PDF_EXTER_PERMISSION) {//存外存權限
int length = Math.min(permissions.length, grantResults.length);
for (int i = 0; i < length; i++) {
if (TextUtils.equals(permissions[i], Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(JSBridge.getActivity(), "下載失敗养泡,請先前往設置打開存儲權限", Toast.LENGTH_SHORT).show();
} else {
startDowloadPdf();//開始下載
}
break;
}
}
}
4、下載代碼
/**
* 下載pdf
*/
private void startDowloadPdf() {
JSONObject returnBody=new JSONObject();
String fileBase64=savePdfJson.getString("fileBase64");
BufferedInputStream bis=null;
FileOutputStream fos=null;
BufferedOutputStream bos=null;
try {
fileBase64=fileBase64.replaceAll("^data:[a-zA-Z]+/[a-zA-Z]+;base64,", "");//此部分因傳過來的數(shù)據(jù)有前綴而寫迄损,可忽略
byte[] bytes=Base64Util.decode(fileBase64);//將給定的Base64編碼的字符串解碼為一個新的字節(jié)數(shù)組定躏,此為自己實現(xiàn)的工具類方法
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes);
bis=new BufferedInputStream(byteArrayInputStream);
Date date=new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String currentData= dateFormat.format(date);
path1=Environment.getExternalStorageDirectory().getPath()+"/"+currentData;
File file=new File(path1+"/"+"xx.pdf");
File path = file.getParentFile();
if (!path.exists()) {
path.mkdirs();//創(chuàng)建多層目錄
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int length = bis.read(buffer);
while (length != -1) {
bos.write(buffer, 0, length);
length = bis.read(buffer);
}
bos.flush();
Toast.makeText(JSBridge.getActivity(), "保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {//失敗
Toast.makeText(JSBridge.getActivity(), "保存失敗", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
fos.close();
} catch (IOException e) {//下載失敗
Toast.makeText(JSBridge.getActivity(),"保存失敗", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
完結(jié)。