首页 热点资讯 义务教育 高等教育 出国留学 考研考公

如何调用android的拍照或本地相册选取,然后再实现相片上传服务器

发布网友 发布时间:2022-04-25 05:23

我来回答

1个回答

热心网友 时间:2023-10-29 18:32

  首先是拍照:使用Intent即可,

  [java] view plaincopyprint?
  01.final String start = Environment.getExternalStorageState();
  02.private static final String PHOTOPATH = "/photo/";
  03.
  04.if(start.equals(Environment.MEDIA_MOUNTED)){
  05.Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  06.File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
  07.if(!file.exists()){
  08.file.mkdirs();
  09.}
  10.tempphontname = System.currentTimeMillis()+".jpg";
  11.buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
  12.intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
  13.startActivityForResult(intent, 1);
  14.}
  final String start = Environment.getExternalStorageState();
  private static final String PHOTOPATH = "/photo/";

  if(start.equals(Environment.MEDIA_MOUNTED)){
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
  if(!file.exists()){
  file.mkdirs();
  }
  tempphontname = System.currentTimeMillis()+".jpg";
  buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
  startActivityForResult(intent, 1);
  }

  

  
  其次是从本地相册选:依旧是Intent.

  如下代码:

  [java] view plaincopyprint?
  01.if(start.equals(Environment.MEDIA_MOUNTED)){
  02. Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
  03. getImage.addCategory(Intent.CATEGORY_OPENABLE);
  04. getImage.setType("image/jpeg");
  05. startActivityForResult(getImage, 0);
  06.}
  if(start.equals(Environment.MEDIA_MOUNTED)){
  Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
  getImage.addCategory(Intent.CATEGORY_OPENABLE);
  getImage.setType("image/jpeg");
  startActivityForResult(getImage, 0);
  }

  接下来是主要的:因为调用完系统的方法后,回返回到回调方法onActivityResult(int, int, Intent)中,

  在里面进行主要的照片上传服务器的操作,

  见代码:

  [java] view plaincopyprint?
  01.@Override
  02. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  03. ContentResolver resolver = getContentResolver();
  04. if(requestCode==1)//
  05. {
  06. if(resultCode==Activity.RESULT_OK)
  07. {
  08. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
  09. {
  10.ew Thread(new Runnable()
  11. {
  12. @Override
  13. public void run() {
  14.//进行上传操作
  15.}
  16.}.start();
转载

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com