主显示布局以及代码:
activity_main.xml:
(总结:这里假设assets和sd卡中的pictures文件夹下有sxt_logo.png这个文件,注意这里要设置外部文件读写权限,以及网络访问权限)
<LinearLayout 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"
android:orientation="vertical" >
<Button
android:id="@+id/btdecodeFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="decodeFile"
/>
<Button
android:id="@+id/btdecodeResource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="decodeResource"
/>
<Button
android:id="@+id/btdecodeStream"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="decodeStream"
/>
<Button
android:id="@+id/btdecodeBytearray"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="btdecodeBytearray"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<Button
android:id="@+id/btClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="btClose"/>
</LinearLayout>
java代码:
package com.example.day0607;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.util.EntityUtils;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
private final static String FILE_NAME = "sxt_logo.png";
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setListener();
}
private void setListener() {
// TODO Auto-generated method stub
resetImage();
setDecodeFile();
setDecodeByearray();
setDecodeResource();
setDecodeStream();
setClose();
}
private void resetImage() {//重置为原来的图片
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
iv.setImageResource(R.drawable.ic_launcher);
}
});
}
private void setClose() {//关闭
// TODO Auto-generated method stub
findViewById(R.id.btClose).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
}
private void setDecodeStream() {//从stream得到Bitmap
findViewById(R.id.btdecodeStream).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
InputStream in = null;
try {
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(dir,FILE_NAME);
in = new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(in);
iv.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
}
private void setDecodeResource() {//从资源中得到Bitmap
findViewById(R.id.btdecodeResource).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
iv.setImageResource(R.drawable.sxt_logo);
}
});
}
private void setDecodeByearray() {//从字节数据中得到Bitmap,这里直接访问servlet,serlvet返回字节数组
findViewById(R.id.btdecodeBytearray).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
HttpGet get = new HttpGet("http://10.0.2.2:8080/ReturnImageServlet/ReturnImageServlet");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
byte[] resultByte = EntityUtils.toByteArray(entity);
final Bitmap bitmap = BitmapFactory.decodeByteArray(resultByte, 0, resultByte.length);
runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
iv.setImageBitmap(bitmap);
}
});
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
});
}
private void setDecodeFile() {//从File中得到Bitmap
findViewById(R.id.btdecodeFile).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(dir,FILE_NAME);
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
iv.setImageBitmap(bitmap);
}
});
}
private void init() {
// TODO Auto-generated method stub
iv = (ImageView) findViewById(R.id.iv);
}
}
列表菜单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.day0607"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
效果:
servlet相关代码:(这里根目录下有image文件夹,文件夹下有sxt_logo.png图片文件)
java代码:
package com.litsoft.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ReturnImageServlet
*/
public class ReturnImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String rootPath = "";
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
rootPath = config.getServletContext().getRealPath("/");//得到根目录
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String filePath = rootPath +"/image/sxt_logo.png";
File file = new File(filePath);
InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] temp = new byte[1024*8];
while(in.read(temp)!=-1){
out.write(temp);
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ReturnImageServlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ReturnImageServlet</display-name>
<servlet-name>ReturnImageServlet</servlet-name>
<servlet-class>com.litsoft.main.ReturnImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ReturnImageServlet</servlet-name>
<url-pattern>/ReturnImageServlet</url-pattern>
</servlet-mapping>
</web-app>