Skip to content

Instantly share code, notes, and snippets.

@JetXing
JetXing / saveMultiparFileToLocal.java
Last active July 31, 2017 08:33
MultipartFile save webapp/img,保存图片到本地
private byte[] readInputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0 ,len);
}
inputStream.close();
return outputStream.toByteArray();
@JetXing
JetXing / FinalVariateChangeByReflection.java
Last active December 7, 2015 02:24
Android 和 Java的java.lang.reflect.Field的API不一致,该方法适用于Android,通过反射方法更改final static 变量,http://stackoverflow.com/网站找到的,自己修改了一下,储存起来
public class FinalVariateChangeByReflection {
public static final String HOST_NAME;
public static final String HOST_NAME_CHANGE = "http://www.apple.com";
static {
HOST_NAME = "http://www.google.com";
}
@JetXing
JetXing / FinalVariateChangeByReflection.java
Last active December 7, 2015 02:24
Android 和 Java的java.lang.reflect.Field的API不一致,该方法适用于Java,通过反射方法更改final static 变量,http://stackoverflow.com/网站找到的,自己修改了一下,储存起来
public class FinalVariateChangeByReflection {
public static final String HOST_NAME;
public static final String HOST_NAME_CHANGE = "http://www.apple.com";
private static final String MODIFY_PERMISSION_FIELD = "modifiers";
static {
HOST_NAME = "http://www.google.com";
@JetXing
JetXing / ImageUtils
Created September 24, 2015 00:58
加载大图,压缩图片,旋转图片
public final static String PICTRUE_SAVE_PATH = Environment.getExternalStorageDirectory() + "/jet/.photo/";
public static String compressImage(String url) {
String path = PICTRUE_SAVE_PATH + String.valueOf(UUID.randomUUID()) + ".jpg";
try {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap image = BitmapFactory.decodeFile(url, newOpts);//此时返回bm为空
@JetXing
JetXing / AudioEncodingBitRate.java
Created September 10, 2015 01:29
AudioEncodingBitRate
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
if (Build.VERSION.SDK_INT >= 10) {
recorder.setAudioSamplingRate(44100);
recorder.setAudioEncodingBitRate(96000);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
} else {
// older version of Android, use crappy sounding voice codec
recorder.setAudioSamplingRate(8000);
@JetXing
JetXing / BackListenerFragment
Created July 2, 2015 03:06
Fragment 处理 back键返回(handle keycode_back in Fragment)
getSupportFragmentManager().beginTransaction().replace(R.id.container, new BackListenerFragment()).addToBackStack("callback").commit();
//inside BackListenerFragment
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
@JetXing
JetXing / IDCard.java
Last active December 3, 2020 14:28
java, 身份证号的正则表达式, regular expression for ID card
package com.gitcafe.android.base.fragment;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.regex.Matcher;
@JetXing
JetXing / ImmersiveMode
Created April 16, 2015 07:49
Immersive Mode(沉浸式) ,设置view的padding
protected final void initStatusBar(int resId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setTintResource(R.color.your_color);
SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
findViewById(resId).setPadding(0, config.getPixelInsetTop(true), 0, config.getPixelInsetBottom());
}
}
@JetXing
JetXing / ActionBarHeight
Created April 15, 2015 06:28
get the height of actionBar
private int getActionBarHeight() {
int actionBarHeight = getSupportActionBar().getHeight();
if (actionBarHeight != 0) {
return actionBarHeight;
}
final TypedValue typedValue = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(
@JetXing
JetXing / ProgressBarWebView
Created April 14, 2015 03:48
add ProgressBar for WebView
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main );
final Activity MyActivity = this;
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{