Android 2种图片(相册--拍照)的上传方式

Android 2种图片(相册–拍照)的上传方式

一 .拍照得到的图片上传

  1. 开始拍照的 ShootActivity
public class ShootActivity extends Activity implements View.OnClickListener {


    private ImageView iv;   //用于显示拍摄后的照片
    private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
    // 创建一个以当前时间为名称的文件
    File tempFile = new File(Environment.getExternalStorageDirectory(), getPhotoFileName());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoot);
        btn = (Button) findViewById(R.id.btn);
        iv = (ImageView) findViewById(R.id.iv);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn) {
            System.out.println("点了拍照存储路径" + tempFile);
    // 拍摄后的图像存储的决定位置 "/storage/emulated/0/QQ/RecentChat/dengziqi.jpg"
            // 调用系统的拍照功能
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // 指定调用相机拍照后照片的储存路径
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
            startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);

        }
    }

//得到返回的数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PHOTO_REQUEST_TAKEPHOTO:
                if (data != null)
                    setPicToView(data);
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //将图片显示到imageView中
    private void setPicToView(Intent picdata) {
        Bundle bundle = picdata.getExtras();
        if (bundle != null) {
            Bitmap photo = bundle.getParcelable("data");
            Drawable drawable = new BitmapDrawable(photo);
            iv.setBackgroundDrawable(drawable);
        }

    }

    // 使用系统当前日期加以调整作为照片的名称
    private String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFromat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
        return dateFromat.format(date) + ".jpg";
    }
}

2.为imageview设置绝对路径的图片(与上述代码无关,仅仅为了测试使用绝对路径显示图片)

 iv = (ImageView) findViewById(R.id.iv);

        BitmapFactory bf = new BitmapFactory();
        final Bitmap bitmap = bf.decodeFile("/storage/emulated/0/QQ/RecentChat/dengziqi.jpg");
        BitmapDrawable drawable = new BitmapDrawable(bitmap);
        iv.setBackground(drawable);

3.上传图片:
当我们拍摄完图片后,我们可以将生成的图片路径 –tempFile 存在一个集合中, 当上传是使用xutils或afinal等框架使用

   RequestParams params = new RequestParams(); 
   params.addBodyParameter("file", new File(tempFile));
   HttpUtils http = new HttpUtils();    
   http.send(HttpRequest.HttpMethod.POST,  
    "uploadUrl....",  //上传的地址
    params,  
    new RequestCallBack<String>() {  

        @Override  
        public void onStart() {  

        }  

        @Override  
        public void onLoading(long total, long current, boolean isUploading) {  

        }  

        @Override  
        public void onSuccess(ResponseInfo<String> responseInfo) {  
        //上传成功
        }  

        @Override  
        public void onFailure(HttpException error, String msg) {  
         //上传失败
        }  
});  

二 .在图库中选择图片上传

1,选择图库的activity

public class PictureActivity extends Activity {
  List<Drawable> mlist;//存储图片的集合
  myAdapter myAdapter;
  GridView mgridView;
  private static final int PHOTO_REQUEST_GALLERY = 1;// 从相册中选择

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_diaplay);
        act = this;
        mlist = new ArrayList<>();
        initView();
    }
// 初始化
    private void initView() {

        top_view = findViewById(R.id.topbar);
        iv_updown = (ImageView) findViewById(R.id.iv_updown);
        mgridView = (GridView) findViewById(R.id.gridView);

        myAdapter = new myAdapter();
        mgridView.setAdapter(myAdapter);
 iv_updown = (Button) findViewById(R.id.iv_updown);
   iv_updown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开启图库
                Intent intent = new Intent(Intent.ACTION_PICK, null);

                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
            }
        });

}

 @Override
    protected void onResume() {
        super.onResume();

        myAdapter.notifyDataSetChanged();


    }
/** * 得到返回的数据 * @param requestCode * @param resultCode * @param data */
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
            case PHOTO_REQUEST_GALLERY:
                if (data != null) {
                    final Uri data1 = data.getData();

                    if (!TextUtils.isEmpty(data1.getAuthority())) {
                        //通过内容提供者 查询选择图片
                        Cursor cursor = getContentResolver().query(
                                data1,
                                new String[]{MediaStore.Images.Media.DATA},
                                null,
                                null,
                                null);
                        //返回 没找到选择图片
                        if (null == cursor) {
                            return;
                        }
                        //光标移动至开头 获取图片路径
                        cursor.moveToFirst();
                        String pathImage = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Images.Media.DATA));

                        System.out.println("获取图片绝对路径 " + pathImage);

                    }
                }
                    break;
                    }
  //将进行剪裁后的图片显示到UI界面上
    private void setPicToView(Intent picdata) {
        Bundle bundle = picdata.getExtras();
        if (bundle != null) {
            Bitmap photo = bundle.getParcelable("data");
            Drawable drawable = new BitmapDrawable(photo);
            // iv.setBackgroundDrawable(drawable);
            mlist.add(drawable);
        }

    }
    }
//填充 GridView 的 adapter
 class myAdapter extends BaseAdapter {
        public myAdapter() {

        }

        @Override
        public int getCount() {
            return mlist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView mimageView;
            if (convertView == null) {
                mimageView = new ImageView(act);
                mimageView.setLayoutParams(new GridView.LayoutParams(150, 150));//设置ImageView对象布局
                mimageView.setPadding(8, 8, 8, 8);//设置间距
            } else {
                mimageView = (ImageView) convertView;
            }
            mimageView.setBackground(mlist.get(position));
            return mimageView;
        }
    }

2.得到图片的路径后,上传图片:
当我们选择完图片后,我们可以将对应的图片路径 –pathImage 存在一个集合中, 当上传是使用xutils或afinal等框架使用

   RequestParams params = new RequestParams(); 
   params.addBodyParameter("file", new File(pathImage));
   HttpUtils http = new HttpUtils();    
   http.send(HttpRequest.HttpMethod.POST,  
    "uploadUrl....",  //上传的地址
    params,  
    new RequestCallBack<String>() {  

        @Override  
        public void onStart() {  

        }  

        @Override  
        public void onLoading(long total, long current, boolean isUploading) {  

        }  

        @Override  
        public void onSuccess(ResponseInfo<String> responseInfo) {  
        //上传成功
        }  

        @Override  
        public void onFailure(HttpException error, String msg) {  
         //上传失败
        }  
});  

结束语:代码没有完全写出来.只是把最核心的代码码了出来.

你可能感兴趣的:(Android 2种图片(相册--拍照)的上传方式)