public class MainActivity extends Activity {
private static final String DATA_PATH="/sdcard/imageShared/";
private String filename;//拍照的文件名
private static final int REQUESTCODE =1;//requestCode标记,做判断时用到
private Button buttonImage,uploadBut;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonImage = (Button) findViewById(R.id.button_Image);
uploadBut =(Button) findViewById(R.id.uploadBut);
iv = (ImageView) findViewById(R.id.imageView1);
initSdcard();
}
/**
* 实现上传功能-未实现
* @author Administrator
*
*/
public void uploadButListener(View v){
}
/**
* 拍照按钮
* 主清单文件添加相应的拍照权限
* <uses-permission android:name="android.permission.CAMERA"/>
* @author Administrator
*
*/
public void ButtonImageListener(View v){
//系统拍照功能:MediaStore.ACTION_IMAGE_CAPTURE
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//filename:生成的图片名称
filename = System.currentTimeMillis()+".jpg";
Uri uri = Uri.fromFile(new File(DATA_PATH+filename));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent,REQUESTCODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getApplicationContext(), "in onActivityResult success", 1).show();
if(resultCode==RESULT_OK && requestCode==REQUESTCODE){
iv.setImageURI(Uri.parse(DATA_PATH+filename));
//让上传按钮显示出来
uploadBut.setVisibility(View.VISIBLE);
}
}
//初始化sdcard信息
private void initSdcard() {
//判断是否有SD卡,没有则创建新的
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = new File(DATA_PATH);
if(!file.exists()){
file.mkdir();
Toast.makeText(getApplicationContext(), "创建sdcard目录", 1).show();
}
}
}
}
拍照-照片-上传
最新推荐文章于 2023-03-25 14:52:00 发布