@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CODE: // If the file selection was successful if (resultCode == RESULT_OK) { if (data != null) { // Get the URI of the selected file final Uri uri = data.getData(); try { // Get the file path from the URI final String path = FileUtils.getPath(this, uri); this.uploadFilePath = path; this.clearLog(); this.writeLog(context.getString(R.string.qiniu_select_upload_file) + path); } catch (Exception e) { Toast.makeText( context, context.getString(R.string.qiniu_get_upload_file_failed), Toast.LENGTH_LONG) .show(); } } } break; } super.onActivityResult(requestCode, resultCode, data); }
private void upload(String uploadToken) { if (this.uploadManager == null) { this.uploadManager = new UploadManager(); } File uploadFile = new File(this.uploadFilePath); String uploadXParam = this.uploadXParamEditText.getText().toString(); Map<String, String> xParams = new HashMap<String, String>(); xParams.put("x:saveKeyEx", uploadXParam); UploadOptions uploadOptions = new UploadOptions( xParams, null, false, new UpProgressHandler() { @Override public void progress(String key, double percent) { updateStatus(percent); } }, null); final long startTime = System.currentTimeMillis(); final long fileLength = uploadFile.length(); this.uploadFileLength = fileLength; this.uploadLastTimePoint = startTime; this.uploadLastOffset = 0; AsyncRun.run( new Runnable() { @Override public void run() { // prepare status uploadPercentageTextView.setText("0 %"); uploadSpeedTextView.setText("0 KB/s"); uploadFileLengthTextView.setText(Tools.formatSize(fileLength)); uploadStatusLayout.setVisibility(LinearLayout.VISIBLE); } }); writeLog(context.getString(R.string.qiniu_upload_file) + "..."); this.uploadManager.put( uploadFile, null, uploadToken, new UpCompletionHandler() { @Override public void complete(String key, ResponseInfo respInfo, JSONObject jsonData) { AsyncRun.run( new Runnable() { @Override public void run() { // reset status uploadStatusLayout.setVisibility(LinearLayout.INVISIBLE); uploadProgressBar.setProgress(0); } }); long lastMillis = System.currentTimeMillis() - startTime; if (respInfo.isOK()) { try { String fileKey = jsonData.getString("key"); String fileHash = jsonData.getString("hash"); writeLog("File Size: " + Tools.formatSize(uploadFileLength)); writeLog("File Key: " + fileKey); writeLog("File Hash: " + fileHash); writeLog("Last Time: " + Tools.formatMilliSeconds(lastMillis)); writeLog("Average Speed: " + Tools.formatSpeed(fileLength, lastMillis)); writeLog("X-Reqid: " + respInfo.reqId); writeLog("X-Via: " + respInfo.xvia); writeLog("--------------------------------"); } catch (JSONException e) { AsyncRun.run( new Runnable() { @Override public void run() { Toast.makeText( context, context.getString(R.string.qiniu_upload_file_response_parse_error), Toast.LENGTH_LONG) .show(); } }); writeLog(context.getString(R.string.qiniu_upload_file_response_parse_error)); if (jsonData != null) { writeLog(jsonData.toString()); } writeLog("--------------------------------"); } } else { AsyncRun.run( new Runnable() { @Override public void run() { Toast.makeText( context, context.getString(R.string.qiniu_upload_file_failed), Toast.LENGTH_LONG) .show(); } }); writeLog(respInfo.toString()); if (jsonData != null) { writeLog(jsonData.toString()); } writeLog("--------------------------------"); } } }, uploadOptions); }