Skip to content

Commit

Permalink
Android压缩图片并调用WebService上传到服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
feicien committed Jan 25, 2013
1 parent 372507d commit 0ee6f1a
Show file tree
Hide file tree
Showing 24 changed files with 550 additions and 0 deletions.
33 changes: 33 additions & 0 deletions FileUploadDemo/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.loveplusplus.demo.fileupload"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<uses-feature android:name="android.hardware.camera" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.loveplusplus.demo.fileupload.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Binary file added FileUploadDemo/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added FileUploadDemo/libs/android-support-v4.jar
Binary file not shown.
Binary file added FileUploadDemo/libs/gson-1.7.1.jar
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions FileUploadDemo/proguard-project.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
14 changes: 14 additions & 0 deletions FileUploadDemo/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-17
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added FileUploadDemo/res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added FileUploadDemo/res/drawable-ldpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added FileUploadDemo/res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added FileUploadDemo/res/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions FileUploadDemo/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/app_name" />

</RelativeLayout>
14 changes: 14 additions & 0 deletions FileUploadDemo/res/menu/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/menu_photo"
android:icon="@drawable/ic_action_photo"
android:showAsAction="always"
android:title="@string/menu_photo"/>
<item
android:id="@+id/menu_upload"
android:icon="@drawable/ic_action_submit"
android:showAsAction="always"
android:title="@string/menu_upload"/>

</menu>
3 changes: 3 additions & 0 deletions FileUploadDemo/res/raw/system.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NAMESPACE=http://file.loveplusplus.com/
METHOD_NAME=upload
URL=http://192.168.1.85:8080/FileUploadWS/FileUploadPort?wsdl
11 changes: 11 additions & 0 deletions FileUploadDemo/res/values-v11/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>

</resources>
12 changes: 12 additions & 0 deletions FileUploadDemo/res/values-v14/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<resources>

<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>

</resources>
8 changes: 8 additions & 0 deletions FileUploadDemo/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">FileUploadDemo</string>
<string name="menu_photo">拍照</string>
<string name="menu_upload">上传</string>

</resources>
20 changes: 20 additions & 0 deletions FileUploadDemo/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<resources>

<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

</resources>
28 changes: 28 additions & 0 deletions FileUploadDemo/src/com/loveplusplus/demo/fileupload/FileBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.loveplusplus.demo.fileupload;

/**
* 附件实体类
*
* @author feicien
*
*/
public class FileBean {
private String fileName;// 文件名
private String fileContent;// 文件的内容

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getFileContent() {
return fileContent;
}

public void setFileContent(String fileContent) {
this.fileContent = fileContent;
}
}
168 changes: 168 additions & 0 deletions FileUploadDemo/src/com/loveplusplus/demo/fileupload/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.loveplusplus.demo.fileupload;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;

public class MainActivity extends Activity {

private String mCurrentPhotoPath;
private static final int REQUEST_TAKE_PHOTO = 0;
public ProgressDialog progressDialog;
private ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView1);
}



@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {

if (requestCode == REQUEST_TAKE_PHOTO) {
if (resultCode == Activity.RESULT_OK) {

//添加到图库,这样可以在手机的图库程序中看到程序拍摄的照片
PictureUtil.galleryAddPic(this,mCurrentPhotoPath);

mImageView.setImageBitmap(PictureUtil
.getSmallBitmap(mCurrentPhotoPath));

} else {
// 取消照相后,删除已经创建的临时文件。
PictureUtil.deleteTempFile(mCurrentPhotoPath);
}
}

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case R.id.menu_photo:
takePhoto();
return true;
case R.id.menu_upload:
upload();
return true;

default:
return super.onOptionsItemSelected(item);
}

}

/**
* 拍照
*/
private void takePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
//指定存放拍摄照片的位置
File f = createImageFile();
takePictureIntent
.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 把程序拍摄的照片放到 SD卡的 Pictures目录中 sheguantong 文件夹中
* 照片的命名规则为:sheqing_20130125_173729.jpg
* @return
* @throws IOException
*/
@SuppressLint("SimpleDateFormat")
private File createImageFile() throws IOException {

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
String timeStamp = format.format(new Date());
String imageFileName = "sheqing_" + timeStamp + ".jpg";

File image = new File(PictureUtil.getAlbumDir(), imageFileName);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}

/**
* 上传到服务器
*/
private void upload() {

if (mCurrentPhotoPath != null) {
FileUploadTask task = new FileUploadTask();
task.execute(mCurrentPhotoPath);
} else {
Toast.makeText(this, "请先点击拍照按钮拍摄照片", Toast.LENGTH_SHORT).show();
}
}

private class FileUploadTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
String filePath = params[0];


FileBean bean = new FileBean();
bean.setFileContent(PictureUtil.bitmapToString(filePath));
File f=new File(filePath);
String fileName=f.getName();
bean.setFileName(fileName);


Gson gson = new Gson();
String json = gson.toJson(bean);

MessageHelper helper = new MessageHelper(MainActivity.this);
return helper.sendMsg(json);
}

@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("正在提交,请稍候...");
progressDialog.show();
}

@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}

}

}
Loading

0 comments on commit 0ee6f1a

Please sign in to comment.