因為此代碼里面有解釋,因此直接上代碼:
public
class ChangeBitmapPixel
extends Activity {
private Button btn;
private Bitmap photo;
private ImageView image;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
private ByteArrayOutputStream baos;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.image);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
// 如果使用下面注釋的代碼,將不返回數據給Intent
// i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
// .getExternalStorageDirectory(),"pic.jpg")));
// 啟動攝像頭並且在拍攝后返回
startActivityForResult(i, 10);
}});
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
// 兩種讀取文件的方法
Uri uri = data.getData();
if (uri != null) {
System.out.println("uri不為空");
photo = BitmapFactory.decodeFile(uri.getPath());
System.out.println("uri:"+photo);
}
if (photo == null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
photo = (Bitmap) bundle.get("data");
image.setImageBitmap(photo);
// 保存照片
savePic(photo);
System.out.println("photo:"+photo);
} else {
Toast.makeText(ChangeBitmapPixel. this,
"為空",
Toast.LENGTH_LONG).show();
return;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void savePic(Bitmap bitmap){
// 使用此流讀取
baos = new ByteArrayOutputStream();
// 第二個參數影響的是圖片的質量,但是圖片的寬度與高度是不會受影響滴
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
// 這個函數能夠設定圖片的寬度與高度
// Bitmap map = Bitmap.createScaledBitmap(bitmap, 400, 400, true);
// 把數據轉為為字節數組
byte[] byteArray = baos.toByteArray();
try {
fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"pic.jpg");
bos = new BufferedOutputStream(fos);
bos.write(byteArray);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private Button btn;
private Bitmap photo;
private ImageView image;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
private ByteArrayOutputStream baos;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.image);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
// 如果使用下面注釋的代碼,將不返回數據給Intent
// i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
// .getExternalStorageDirectory(),"pic.jpg")));
// 啟動攝像頭並且在拍攝后返回
startActivityForResult(i, 10);
}});
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
// 兩種讀取文件的方法
Uri uri = data.getData();
if (uri != null) {
System.out.println("uri不為空");
photo = BitmapFactory.decodeFile(uri.getPath());
System.out.println("uri:"+photo);
}
if (photo == null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
photo = (Bitmap) bundle.get("data");
image.setImageBitmap(photo);
// 保存照片
savePic(photo);
System.out.println("photo:"+photo);
} else {
Toast.makeText(ChangeBitmapPixel. this,
"為空",
Toast.LENGTH_LONG).show();
return;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void savePic(Bitmap bitmap){
// 使用此流讀取
baos = new ByteArrayOutputStream();
// 第二個參數影響的是圖片的質量,但是圖片的寬度與高度是不會受影響滴
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
// 這個函數能夠設定圖片的寬度與高度
// Bitmap map = Bitmap.createScaledBitmap(bitmap, 400, 400, true);
// 把數據轉為為字節數組
byte[] byteArray = baos.toByteArray();
try {
fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"pic.jpg");
bos = new BufferedOutputStream(fos);
bos.write(byteArray);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}