压缩图片
package com.test.demo;
import java.io.File;
import java.util.Calendar;
import com.test.viewdemo.R;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.OnHierarchyChangeListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn_camera;
private LinearLayout linear;
private MainActivity _this;
private Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_this = this;
btn_camera = (Button) findViewById(R.id.button1);
linear = (LinearLayout) _this.findViewById(R.id.image_linear);
String path = Environment.getExternalStorageDirectory() + "/image";
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
String time = Calendar.getInstance().getTime().toString();
File mediaFile = new File(file.getAbsolutePath(),time + ".jpg");
fileUri = Uri.fromFile(mediaFile);
btn_camera.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == _this.RESULT_OK){
if(requestCode == 100){
ImageView image = new ImageView(_this);
LinearLayout.LayoutParams llparams = new LinearLayout.LayoutParams(80, 80);
image.setLayoutParams(llparams);
if(data != null){
if(data.hasExtra("data")){
Bitmap thumbnail = data.getParcelableExtra("data");
image.setImageBitmap(thumbnail);
}
}else{
int width = image.getLayoutParams().width;
int height = image.getLayoutParams().height;
BitmapFactory.Options option = new Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileUri.getPath(), option);
int imageW = option.outWidth;
int imageH = option.outHeight;
int scale = Math.max(imageW / width, imageH / height);
option.inJustDecodeBounds = false;
option.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), option);
image.setImageBitmap(bitmap);
linear.addView(image);
if(linear != null){
if(linear.getChildCount() > 0){
linear.setVisibility(View.VISIBLE);
}else{
linear.setVisibility(View.GONE);
}
}
}
}
}
}
}