当前位置: 编程技术>移动开发
本页文章导读:
▪手机照片下传 手机照片上传
首先写好一个专门用于封装提交的信息的处理类
FormFile.java
package com.cnjmwl.util;
public class FormFile
{
/* 上传文件的数据 */
private byte[] data;
/* 文件名称 */
private String fi.........
▪ 【转】PendingIntent跟Intent的区别 【转】PendingIntent和Intent的区别
Notification n = new Notification(R.drawable.face_1, "Service启动", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TServiceHolder.class), 0); .........
▪ EditeText标签名体大小设置 是否可编辑 EditeText标签字体大小设置 是否可编辑
<EditText android:layout_width="180px" android:editable="false" android:layout_height="wrap_content" android:textColor="@drawable/Color_black" android:id="@+id/clsx" andr.........
[1]手机照片下传
来源: 互联网 发布时间: 2014-02-18
手机照片上传
首先写好一个专门用于封装提交的信息的处理类
FormFile.java
package com.cnjmwl.util;
public class FormFile
{
/* 上传文件的数据 */
private byte[] data;
/* 文件名称 */
private String filname;
/* 表单字段名称*/
private String formname;
/* 内容类型 */
private String contentType = "text/plain"; //需要查阅相关的资料
public FormFile(String filname, byte[] data, String formname, String contentType) {
this.data = data;
this.filname = filname;
this.formname = formname;
if(contentType!=null) this.contentType = contentType;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public String getFilname() {
return filname;
}
public void setFilname(String filname) {
this.filname = filname;
}
public String getFormname() {
return formname;
}
public void setFormname(String formname) {
this.formname = formname;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}
然后写android客户端提交到的方法
String actionURl="http://192.168.1.79:6888/jmcustomer/placeOrderServlet";
//String actionURl=HttpUtil.BASE_URL+"new";
Map<String, String> params=new HashMap<String, String>();
//传个用户名
params.put("username", this.getIntent().getStringExtra("username"));
params.put("orderdesc", StringUtil.utf8ToUnicode(orderdesc.getText().toString()));
String contentType = "image/JPEG";
File file =new File("/data/data/test.jpg");
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
BufferedInputStream bufferedInputStream;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
while ((len = bufferedInputStream.read(buffer)) != -1)
{
outStream.write(buffer, 0, len);
}
data = outStream.toByteArray();
outStream.close();
bufferedInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//获取用户编辑照片的文件名
String photoName=fileName.getText().toString();
if(photoName==null||photoName.trim().equals("")){
photoName="default";
}
// FormFile[] files=new FormFile[]{new FormFile("test.jpg",data,"file1",contentType)};
FormFile[] files=new FormFile[]{new FormFile(photoName+".jpg",mContent,"file1",contentType)};
String result= post(actionURl, params, files);
if(result.equals("1")){
showAlert("提交成功!");
}else{
showAlert("提交失败!");
}
/**
* 直接通过HTTP协议提交数据到服务器,实现表单提交功能
* @param actionUrl 上传路径
* @param params 请求参数 key为参数名,value为参数值
* @param file 上传文件
*/
public static String post(String actionUrl, Map<String, String> params, FormFile[] files) {
try {
String BOUNDARY = "---------7d4a6d158c9"; //数据分隔线
String MULTIPART_FORM_DATA = "multipart/form-data";
URL url = new URL(/blog_article/actionUrl/index.html);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);//允许输入
conn.setDoOutput(true);//允许输出
conn.setUseCaches(false);//不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
StringBuilder sb = new StringBuilder();
//上传的表单参数部分,格式请参考文章
for (Map.Entry<String, String> entry : params.entrySet()) {//构建表单字段内容
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
sb.append(entry.getValue());
sb.append("\r\n");
}
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());//发送表单字段数据
//上传的文件部分,格式请参考文章
for(FormFile file : files){
StringBuilder split = new StringBuilder();
split.append("--");
split.append(BOUNDARY);
split.append("\r\n");
split.append("Content-Disposition: form-data;name=\""+ file.getFormname()+"\";filename=\""+ file.getFilname() + "\"\r\n");
split.append("Content-Type: "+ file.getContentType()+"\r\n\r\n");
outStream.write(split.toString().getBytes());
outStream.write(file.getData(), 0, file.getData().length);
outStream.write("\r\n".getBytes());
}
byte[] end_data = ("--" + BOUNDARY + "--\r\n").getBytes();//数据结束标志
outStream.write(end_data);
outStream.flush();
int cah = conn.getResponseCode();
if (cah != 200) throw new RuntimeException("请求url失败");
InputStream is = conn.getInputStream();
int ch;
StringBuilder b = new StringBuilder();
while( (ch = is.read()) != -1 ){
b.append((char)ch);
}
outStream.close();
conn.disconnect();
return b.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
[2] 【转】PendingIntent跟Intent的区别
来源: 互联网 发布时间: 2014-02-18
【转】PendingIntent和Intent的区别
PendingIntent和Intent的区别:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.
1. GSM网络中android发送短信示例
(1)代码节选
2)代码解释
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去 。
函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
1)PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
2)PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的PendingIntent广播。
查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。
原文地址 http://wayfarer.iteye.com/blog/586159
Notification n = new Notification(R.drawable.face_1, "Service启动", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TServiceHolder.class), 0); n.setLatestEventInfo(this, "任务标题", "任务内容", contentIntent); nManager.notify(NOTIFICATION_ID, n); // 任务栏启动
PendingIntent和Intent的区别:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.
1. GSM网络中android发送短信示例
(1)代码节选
String msg ="你好,美女"; String number = "135****6784"; SmsManager sms = SmsManager.getDefault(); PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0); sms.sendTextMessage(number, null, msg, pi, null); Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();
2)代码解释
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去 。
函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
1)PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
2)PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的PendingIntent广播。
查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。
原文地址 http://wayfarer.iteye.com/blog/586159
[3] EditeText标签名体大小设置 是否可编辑
来源: 互联网 发布时间: 2014-02-18
EditeText标签字体大小设置 是否可编辑
<EditText android:layout_width="180px" android:editable="false"
android:layout_height="wrap_content" android:textColor="@drawable/Color_black" android:id="@+id/clsx" android:textSize="16px">
</EditText>
android:textSize="16px"-----字体大小设置
android:editable="false"-----不可编辑
最新技术文章: