MainActivity
- 格式:docx
- 大小:15.20 KB
- 文档页数:3
package com.example.androidcamera;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import .Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
File file = null;
Bitmap photoBitmap = null;
Button button = null;
ImageView imageView = null;
String pictureDir = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.activity_main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
destoryBimap();
String state = Environment.getExternalStorageState();
if(state.equals(Environment.MEDIA_MOUNTED)){
String savDir = Environment.getExternalStorageDirectory() + "/temple";
File dir = new File(savDir);
if(!dir.exists()){
dir.mkdir();
}
file = new File(savDir, "temp.jpg");
file.delete();
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this,"已经存在",
Toast.LENGTH_LONG).show();
return;
}
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 1);
}else{
Toast.makeText(MainActivity.this,
"不存在内存卡", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
/*if(requestCode != 2){
return;
}*/
if(file != null && file.exists()){
BitmapFactory.Options bitmapFactory = new BitmapFactory.Options();
bitmapFactory.inSampleSize = 2;
photoBitmap = BitmapFactory.decodeFile(file.getPath(),bitmapFactory);
//imageView.setBackgroundDrawable(new
BitmapDrawable(photoBitmap));
imageView.setImageBitmap(photoBitmap);
pictureDir = file.getPath();
}else{
Toast.makeText(this, "文件已存在", Toast.LENGTH_LONG).show();
}
}
}
private void destoryBimap() {
if (photoBitmap != null && !photoBitmap.isRecycled()) { photoBitmap.recycle();
photoBitmap = null;
}
}
}。