根据本地相片路径上传图片到服务器的2中方法

package com.util;import java.io.ByteArrayOutputStream;import java.io.File;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Environment;import android.util.Base64;/*** 根据本地相片路径上传图片到服务器的2中方法* @author yjbo*2015-07-25 13:37:14*/public class PictureUtil {/*** 上传图片时:把bitmap转换成String* 这2种方法有优缺点:* 1)法一(控制长宽的):这个正常拍的1-2M的照片只会压缩到400kb左右,但是画质也的确清楚;然后拍完照也不会黑屏;* 2)法二(控制大小(kb字节的)):这个就是控制照片大小在100kb以内,,不改变照片原来的长宽,然后拍完照会黑屏(应该是循环次数多了);相对而言除了这种方案不好,服务器中不是很清楚* @param filePath* @return*/public static String bitmapToString(String filePath) {Bitmap bitmap ;//filePathOri = filePath;//==图片路径==/storage/sdcard0/DCIM/Camera/IMG_20141119_122410p1.jpg//System.out.println("==图片路径=="+filePathOri);ByteArrayOutputStream baos = new ByteArrayOutputStream();//法一:这是控制图片的长宽的:Bitmap bm = getSmallBitmap(filePath);bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);//法二:压缩图片:这是控制图片的大小的(100kb以内):try {BitmapFactory.Options options = new BitmapFactory.Options();bitmap = BitmapFactory.decodeFile(filePath, options);int options1 = 100;try {bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}while (baos.toByteArray().length / 1024 > 100) {//原图都是基本1-2M;if(options1<10){//大约公约数的2倍break;//这是因为有可能options1已经为0了,但是还没到100kb以内}baos.reset();options1 -= 5;bitmap.compress(Bitmap.CompressFormat.JPEG, options1, baos);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}byte[] b = baos.toByteArray();return Base64.encodeToString(b, Base64.DEFAULT);}/*** 根据路径获得突破并压缩返回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 inSampleSizeoptions.inSampleSize = calculateInSampleSize(options, 480, 800);// Decode bitmap with inSampleSize setoptions.inJustDecodeBounds = false;return BitmapFactory.decodeFile(filePath, options);}/*** 计算图片的缩放?** @param options* @param reqWidth* @param reqHeight* @return*/public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {// Raw height and width of imagefinal int height = options.outHeight;final int width = options.outWidth;int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {// widthfinal 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;}/*** 根据路径删除图片** @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());if (!dir.exists()) {dir.mkdirs();}return dir;}/*** 获取保存 隐患?的图片文件夹名称** @return*/public static String getAlbumName() {return "sheguantong";//==图片路径==/storage/sdcard0/DCIM/Camera/IMG_20141119_122410p1.jpg//String filepathPp = null;//String filePathOri2 = filePathOri.substring(19, 25);////if(filePathOri.contains("p0")){//filepathPp= filePathOri2.replace("p0.jpg", "pic1_1");//}else if(filePathOri.contains("p1")){//filepathPp= filePathOri2.replace("p1.jpg", "pic2_1");//}else if(filePathOri.contains("p2")){//filepathPp= filePathOri2.replace("p2.jpg", "pic3_1");//}else if(filePathOri.contains("p3")){//filepathPp= filePathOri2.replace("p3.jpg", "pic4_1");//}//return ;}}

版权声明:本文为博主原创文章,未经博主允许不得转载。

人生不能缺乏的是雨夜--淅沥地,独自的雨夜。这样的雨夜里,

根据本地相片路径上传图片到服务器的2中方法

相关文章:

你感兴趣的文章:

标签云: