主程序
package com.test.myphotoshop;
import android.support.v7.app.ActionBarActivity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.audiofx.EnvironmentalReverb;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener{
public static final int TAKE_PHOTO=1;
public static final int CROP_PHOTO=2;
private Button mButtonTakePhoto;
private Button mChooseFromAlbum;
private ImageView mPicture;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonTakePhoto=(Button) findViewById(R.id.button_take_photo);
mChooseFromAlbum=(Button) findViewById(R.id.button_choose_photo);
mChooseFromAlbum.setOnClickListener(this);
mPicture=(ImageView) findViewById(R.id.picture);
mButtonTakePhoto.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if(resultCode==RESULT_OK){
Intent intent =new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);
}
break;
case CROP_PHOTO:
if(resultCode==RESULT_OK){
try {
Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
mPicture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
default:
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_take_photo:
File outputImage=new File(Environment.getExternalStorageDirectory(),"tempImage.jpg");
try {
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri=Uri.fromFile(outputImage);
Intent intent =new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, TAKE_PHOTO);
break;
case R.id.button_choose_photo:
File outputImage2 =new File(Environment.getExternalStorageDirectory(),"output_image.jpg");
try {
if(outputImage2.exists()){
outputImage2.delete();
}
outputImage2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri=Uri.fromFile(outputImage2);
Intent intent2=new Intent("android.intent.action.GET_CONTENT");
intent2.setType("image/*");
intent2.putExtra("crop", true);
intent2.putExtra("scale", true);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent2, CROP_PHOTO);
break;
default:
break;
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button_take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Take Photo" />
<Button
android:id="@+id/button_choose_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose From Album" />
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
图片压缩代码
主要是第一个方法,其他两个方法不用管,下面两个图片压缩方法是google提供的,想要压缩图片是只要把图片的路径传入即可。
private void zipImage(String savePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savePath, options);
options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
try {
FileOutputStream fos = new FileOutputStream(savePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
bitmap = null;
System.gc();
}
public int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
最终版本
主程序
package com.test.myphotoshop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.audiofx.EnvironmentalReverb;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2;
public static final int TEACHER_PHOTO = 3;
public static final int CHOOSE_PHOTO=4;
private Button mButtonTakePhoto;
private Button mChooseFromAlbum;
private Button mTeacherPhoto;
private ImageView mPicture;
private File newfile;
private Uri imageUri;
private Uri dataPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonTakePhoto = (Button) findViewById(R.id.button_take_photo);
mChooseFromAlbum = (Button) findViewById(R.id.button_choose_photo);
mChooseFromAlbum.setOnClickListener(this);
mPicture = (ImageView) findViewById(R.id.picture);
mButtonTakePhoto.setOnClickListener(this);
mTeacherPhoto = (Button) findViewById(R.id.button_Teacher_photo);
mTeacherPhoto.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);
}
break;
case CROP_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
mPicture.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
case TEACHER_PHOTO:
if (resultCode == 8) {
ImageZip.zipImage(newfile.getAbsolutePath());
mPicture.setImageURI(Uri.fromFile(newfile));
}
break;
case CHOOSE_PHOTO:
if (resultCode == RESULT_OK) {
dataPhoto=data.getData();
String[] proj={MediaStore.Images.Media.DATA};
Cursor actualimagecursor=managedQuery(dataPhoto, proj, null, null, null);
int actual_image_column_index=actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path=actualimagecursor.getString(actual_image_column_index);
File file =new File(img_path);
ImageZip.zipImage(file.getAbsolutePath());
mPicture.setImageURI(Uri.fromFile(file));
}
break;
default:
break;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_take_photo:
File outputImage = new File(Environment.getExternalStorageDirectory(), "tempImage.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
break;
case R.id.button_choose_photo:
Intent intent2 = new Intent("android.intent.action.GET_CONTENT");
intent2.setType("image/*");
startActivityForResult(intent2, CHOOSE_PHOTO);
break;
case R.id.button_Teacher_photo:
Intent intent3 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
newfile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
if (!newfile.exists()) {
try {
newfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
setResult(8);
intent3.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newfile));
startActivityForResult(intent3, TEACHER_PHOTO);
break;
default:
break;
}
}
}
压缩图片的程序
package com.test.myphotoshop;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImageZip {
public static void zipImage(String savePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savePath, options);
options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
try {
FileOutputStream fos = new FileOutputStream(savePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
bitmap = null;
System.gc();
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
public static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button_take_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Take Photo" />
<Button
android:id="@+id/button_choose_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose From Album" />
<Button
android:id="@+id/button_Teacher_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Teacher Photo" />
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>