这几天有做到一个小的案例,手机拍照、相册照片上传到服务器。客户端和服务器的代码都贴出来:
客户端
AndroidManifest.xml
添加以下权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
客户端的上传图片activity_upload.xml
布局文件
<?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"/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_margin="16dp"
android:background="@drawable/edit_text_bg"/>
<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>
UploadActivity.java
界面代码
package com.eric.uploadimage;
import android.annotation.SuppressLint;