android图片上传

本文介绍了在Android Studio项目中实现图片上传功能的步骤,包括布局设计、选择图片(相册和拍照)、64位编码转换及使用android-async-http库进行上传。在Java后台,通过Servlet接收图片。在实现过程中遇到Header[]类找不到的问题,已找到解决方案。测试时发现,对于大尺寸图片(如本机拍摄的4M照片),后台无法接收,提示需要对图片进行压缩处理。
摘要由CSDN通过智能技术生成

最近项目上需要图片上传功能,所以开始查询相关资料,并终于实现。

android这边使用得是android studio,后台处理这边使用得是eclipse中servlet

首先是布局:(比较简单)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>

    <Button
        android:id="@+id/choose_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择图片"/>

    <Button
        android:id="@+id/upload_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="上传图片"/>

</LinearLayout>

然后是activity

相册和拍照2种方式

 final CharSequence[] items = {"相册", "拍照"};
        AlertDialog.Builder dlg = new AlertDialog.Builder(UploadActivity.this);
        dlg.setTitle("添加图片");
        dlg.setTitle("添加图片");
        dlg.setItems(items, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int item) {
                 // 这里item是根据选择的方式,
                 if (item == 0) {
                     Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                     startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
                     } else {
                       try {
                         Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                         startActivityForResult(intent, 0);
                         } catch (Exception e) {
                         e.
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值