public class VideoUtil { private static final String TAG = "VideoUtil"; public static String videoToString(String filePath) { try { File tmpFile = new File(filePath); BufferedInputStream in = new BufferedInputStream(new FileInputStream(tmpFile)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[in.available()]; int length; while ((length = in.read(buffer)) != -1) { baos.write(buffer, 0, length); } byte[] b = baos.toByteArray(); return Base64.encodeToString(b, Base64.DEFAULT); } catch (Exception e) { Log.e(TAG, "讀取視頻出現異常", e); } return null; } }
public class PictureUtil { /** * 把bitmap轉換成String * * @param filePath * @return */ public static String bitmapToString(String filePath) { Bitmap bm = getSmallBitmap(filePath); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 40, baos); byte[] b = baos.toByteArray(); return Base64.encodeToString(b, Base64.DEFAULT); } /** * 計算圖片的縮放值 * * @param options * @param reqWidth * @param reqHeight * @return */ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } /** * 根據路徑得到突破並壓縮返回bitmap用於顯示 * * @param imagesrc * @return */ public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 200, 200); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } /** * 根據路徑刪除圖片 * * @param path */ public static void deleteTempFile(String path) { File file = new File(path); if (file.exists()) { file.delete(); } } /** * 添加到圖庫 */ public static void galleryAddPic(Context context, String path) { Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(path); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri); context.sendBroadcast(mediaScanIntent); } /** * 獲取保存圖片的目錄 * * @return */ public static File getAlbumDir() { // File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getAlbumName()); File dir = new File(Environment.getExternalStorageDirectory(), getAlbumName()); if (!dir.exists()) { dir.mkdirs(); } return dir; } /** * 獲取保存 隱患檢查的圖片文件夾名稱 * * @return */ public static String getAlbumName() { return Environment.getExternalStorageDirectory()+"/TMS/"; } }
public class MessageHelper { private static final String TAG = null; private static String NAMESPACE = null; private static String URL = null; private static String METHOD_NAME = null; private static String SOAP_ACTION = null; private static String POST_URL = null; public MessageHelper(Context context) { try { Properties p = new Properties(); p.load(context.getResources().openRawResource(R.raw.system)); NAMESPACE = p.getProperty("NAMESPACE"); URL = p.getProperty("URL"); METHOD_NAME = p.getProperty("METHOD_NAME"); SOAP_ACTION = NAMESPACE + METHOD_NAME; POST_URL=p.getProperty("POST_URL"); } catch (NotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 調用webservice * * @param json * @return */ public String sendMsg(String json) { try { SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); rpc.addProperty("arg0", json); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet = false; envelope.encodingStyle = "UTF-8"; envelope.setOutputSoapObject(rpc); new MarshalBase64().register(envelope); HttpTransportSE aht = new HttpTransportSE(URL, 60 * 1000); aht.call(SOAP_ACTION, envelope); Object result = (Object) envelope.getResponse(); Log.d(TAG, result.toString()); return String.valueOf(result); } catch (Exception e) { e.printStackTrace(); } return null; } public String sendPost(String json) { try { HttpURLConnection httpcon = (HttpURLConnection) ((new URL(POST_URL) .openConnection())); httpcon.setDoOutput(true); httpcon.setRequestProperty("Content-Type", "application/json"); httpcon.setRequestProperty("Accept", "application/json"); httpcon.setRequestMethod("POST"); httpcon.connect(); byte[] outputBytes = json.getBytes("UTF-8"); OutputStream os = httpcon.getOutputStream(); os.write(outputBytes); os.close(); int status = httpcon.getResponseCode(); if (status != 200) { throw new IOException("Post failed with error code " + status); } BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line+"\n"); } br.close(); return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } }
/** * 附件實體類 * * @author feicien * */ public class FileBean { private String fileName;// 文件名 private String fileContent;// 文件的內容 public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileContent() { return fileContent; } public void setFileContent(String fileContent) { this.fileContent = fileContent; } }