Upload an image to server 2 based on the local photo path.

Source: Internet
Author: User

Upload an image to server 2 based on the local photo path.
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;

/**
* How to upload images to server 2 based on the local photo path
* @ Author yjbo
* 13:37:14
*/
Public class PictureUtil {
/**
* When uploading an image: Convert bitmap to String
* These two methods have advantages and disadvantages:
* 1) method 1 (with a length and width controlled): This Normal 1-2 m photo will only be compressed to about KB, but the image quality is clear. After the photo is taken, the screen will not be black;
* 2) method 2 (Size Control (kb): this means that the size of the photo is limited to kb without changing the original length and width of the photo, then, the black screen (the number of cycles should be too large) is taken. In contrast, this solution is not good, and the server is not very clear.
* @ Param filePath
* @ Return
*/

Public static String bitmapToString (String filePath ){
Bitmap bitmap;
// FilePathOri = filePath;
/// = Image Path =/storage/sdcard0/DCIM/Camera/IMG_20141119_122410p1.jpg
// System. out. println ("= image Path =" + filePathOri );
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
// Method 1: This controls the image length and width:
Bitmap bm = getSmallBitmap (filePath );
Bm. compress (Bitmap. CompressFormat. JPEG, 100, baos );

// Method 2: compress the image: this is the size of the image (within KB ):
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 block
E. printStackTrace ();
}
While (baos. toByteArray (). length/1024> 100) {// The Source image is basically 1-2 M;

If (options1 <10) {// approximately 2 times the approximate number of public approx.
Break; // This is because options1 is already 0, but not within kb.
}
Baos. reset ();
Options1-= 5;
Bitmap. compress (Bitmap. CompressFormat. JPEG, options1, baos );
}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}


Byte [] B = baos. toByteArray ();


Return Base64.encodeToString (B, Base64.DEFAULT );


}


/**
* Break through the path and compress the returned bitmap for display
*
* @ 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, 480,800 );


// Decode bitmap with inSampleSize set
Options. inJustDecodeBounds = false;


Return BitmapFactory. decodeFile (filePath, options );
}


/**
* Calculate the scaling of an image?
*
* @ 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 ){


// 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
// Requested height and width.
InSampleSize = heightRatio <widthRatio? HeightRatio: widthRatio;
}


Return inSampleSize;
}


/**
* Delete an image by path
*
* @ Param path
*/
Public static void deleteTempFile (String path ){
File file = new File (path );
If (file. exists ()){
File. delete ();
}
}


/**
* Add to graph?
*/
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 );
}


/**
* Get the object for saving the image?
*
* @ Return
*/
Public static File getAlbumDir (){
File dir = new File (Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_PICTURES), getAlbumName ());
If (! Dir. exists ()){
Dir. mkdirs ();
}
Return dir;
}


/**
* Is there a hidden danger of saving? �� Image folder name
*
* @ Return
*/
Public static String getAlbumName (){
Return "sheguw.g ";
/// = Image Path =/storage/sdcard0/DCIM/Camera/IMG_20141119_122410p1.jpg
// String filepathPp = null;
// String filePathOri2 = filePathOri. substring (19, 25 );
//
// If (filePathOri. contains ("p0 ")){
// FilepathPp = filePathOri2.replace ("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;
}
}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.