android 得到本机上的一张图路径,在线等 android中,需在一界面上,有一“选择图片”的 按纽,点击此按纽后,可选择本机中的相片,再选中 一张相片,再把此 相片的路径可用 System.out.println(url); 输出来现问:1 xml布局如何写 2 程序代码如何写thanks 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 回3楼 要取手机中的图片回2楼 我看了你的这bolg http://blog.csdn.net/raphael55/article/details/6838018 能把此源代码发我邮件吗 [email protected]多谢 package com.webview;import java.io.FileNotFoundException; import com.http.HTTPRequestHelper;import android.app.Activity; import android.app.AlertDialog;import android.content.ContentResolver; import android.content.DialogInterface;import android.content.Intent; import android.database.Cursor;import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment;import android.os.Handler;import android.provider.MediaStore;import android.util.Log; import android.view.MotionEvent;import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.Date;import java.text.SimpleDateFormat;import java.util.Locale;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.io.FileNotFoundException;import android.app.Activity;import android.app.AlertDialog;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.provider.MediaStore;import android.util.Log;import android.widget.ImageView;public class Pic extends Activity { private String newName="image.jpg"; //private String uploadFile="/mnt/sdcard/DCIM/Camera/Icon-Small.png"; private String actionUrl="http://nc.8ff.cn/android_upload_o.php"; private String path; private Uri uri; private final int REQ_CODE_CAMERA =1; private final int REQ_CODE_PICTURE =0; private String imgurl; private File tempfile; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pic); Button bpic = (Button)findViewById(R.id.bpic); bpic.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { /* Intent i = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(i, REQ_CODE_CAMERA); */ ImageCapture(); } }); Button button = (Button)findViewById(R.id.b01); button.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(); /* 开启Pictures画面Type设定为image */ intent.setType("image/*"); /* 使用Intent.ACTION_GET_CONTENT这个Action */ intent.setAction(Intent.ACTION_GET_CONTENT); /* 取得相片后返回本画面 */ startActivityForResult(intent, REQ_CODE_PICTURE); } }); } public void ImageCapture() { File DatalDir = Environment.getExternalStorageDirectory(); File myDir = new File(DatalDir, "/DCIM/Camera"); myDir.mkdirs(); String mDirectoryname = DatalDir.toString() + "/DCIM/Camera"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hhmmss", Locale.SIMPLIFIED_CHINESE); tempfile = new File(mDirectoryname, sdf.format(new Date()) + ".jpg"); if (tempfile.isFile()) tempfile.delete(); Uri Imagefile = Uri.fromFile(tempfile); Intent cameraIntent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Imagefile); startActivityForResult(cameraIntent, REQ_CODE_CAMERA); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if(requestCode==REQ_CODE_CAMERA) { System.out.println("ar"+tempfile.getPath()); uploadFile(tempfile.getPath()); } else if(requestCode==REQ_CODE_PICTURE) { uri = data.getData(); path= getAbsoluteImagePath(uri); System.out.println("a"+path); uploadFile(path); } } } public void onLongPress(MotionEvent e) { } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { return false; } public void onShowPress(MotionEvent e) { } public boolean onSingleTapUp(MotionEvent e) { return false; } private Handler handlers = new Handler(){ public void dispatchMessage(android.os.Message msg) { return; }; }; protected String getAbsoluteImagePath(Uri uri) { // can post image String [] proj={MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery( uri, proj, // Which columns to return null, // WHERE clause; which rows to return (all rows) null, // WHERE clause selection arguments (none) null); // Order-by clause (ascending by name) int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } /* 上传文件吹Server的method */ private void uploadFile(String path) { String end = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; try { URL url =new URL(actionUrl); HttpURLConnection con=(HttpURLConnection)url.openConnection(); /* 允许Input、Output,不使用Cache */ con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); /* 设定传送的method=POST */ con.setRequestMethod("POST"); /* setRequestProperty */ con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Charset", "UTF-8"); con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); /* 设定DataOutputStream */ DataOutputStream ds = new DataOutputStream(con.getOutputStream()); ds.writeBytes(twoHyphens + boundary + end); ds.writeBytes("Content-Disposition: form-data; " + "name=\"uploadfile\";filename=\"" + newName +"\"" + end); ds.writeBytes(end); /* 取得文件的FileInputStream */ FileInputStream fStream = new FileInputStream(path); /* 设定每次写入1024bytes */ int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int length = -1; /* 从文件读取数据到缓冲区 */ while((length = fStream.read(buffer)) != -1) { /* 将数据写入DataOutputStream中 */ ds.write(buffer, 0, length); } ds.writeBytes(end); ds.writeBytes(twoHyphens + boundary + twoHyphens + end); /* close streams */ fStream.close(); ds.flush(); /* 取得Response内容 */ InputStream is = con.getInputStream(); int ch; StringBuffer b =new StringBuffer(); while( ( ch = is.read() ) != -1 ) { b.append( (char)ch ); } /* 将Response显示于Dialog */ showDialog(b.toString().trim()); /* 关闭DataOutputStream */ ds.close(); } catch(Exception e) { showDialog(""+e); } } /* 显示Dialog的method */ private void showDialog(String mess) { new AlertDialog.Builder(Pic.this).setTitle("Message") .setMessage(mess) .setNegativeButton("确定",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }) .show(); } //class } 安卓camera框架是怎么与V4L2驱动连接起来的 请教一个Android API 帮助文档的问题 listview中按钮状态的保存,希望能过获得思路 求助高手!我的线程运行时间不同! 继续求解XML解析,为何在不存在问题的地方出了错,求教高手指点 怎么提取出输入框Dialog中的内容? arcgis android android 进行DOM解析XML文件的问题 Android开发小问题,请帮忙解决! android 调用webservice没有查询结果 android监听软键盘问题 超级难题,搜索不到任何答案,至少求助大侠们
回2楼 我看了你的这bolg http://blog.csdn.net/raphael55/article/details/6838018
能把此源代码发我邮件吗 [email protected]
多谢
import com.http.HTTPRequestHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.app.AlertDialog;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;public class Pic extends Activity
{
private String newName="image.jpg";
//private String uploadFile="/mnt/sdcard/DCIM/Camera/Icon-Small.png";
private String actionUrl="http://nc.8ff.cn/android_upload_o.php";
private String path;
private Uri uri;
private final int REQ_CODE_CAMERA =1;
private final int REQ_CODE_PICTURE =0;
private String imgurl;
private File tempfile;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pic);
Button bpic = (Button)findViewById(R.id.bpic);
bpic.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
/*
Intent i = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, REQ_CODE_CAMERA);
*/
ImageCapture();
}
});
Button button = (Button)findViewById(R.id.b01);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, REQ_CODE_PICTURE);
}
});
}
public void ImageCapture() {
File DatalDir = Environment.getExternalStorageDirectory();
File myDir = new File(DatalDir, "/DCIM/Camera");
myDir.mkdirs();
String mDirectoryname = DatalDir.toString() + "/DCIM/Camera";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hhmmss",
Locale.SIMPLIFIED_CHINESE);
tempfile = new File(mDirectoryname, sdf.format(new Date())
+ ".jpg");
if (tempfile.isFile())
tempfile.delete();
Uri Imagefile = Uri.fromFile(tempfile);
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Imagefile);
startActivityForResult(cameraIntent, REQ_CODE_CAMERA);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if(requestCode==REQ_CODE_CAMERA)
{
System.out.println("ar"+tempfile.getPath());
uploadFile(tempfile.getPath());
}
else if(requestCode==REQ_CODE_PICTURE)
{
uri = data.getData();
path= getAbsoluteImagePath(uri);
System.out.println("a"+path);
uploadFile(path);
}
}
} public void onLongPress(MotionEvent e) {
} public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
return false;
} public void onShowPress(MotionEvent e) {
} public boolean onSingleTapUp(MotionEvent e) {
return false;
}
private Handler handlers = new Handler(){
public void dispatchMessage(android.os.Message msg) {
return;
};
};
protected String getAbsoluteImagePath(Uri uri)
{
// can post image
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( uri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
/* 上传文件吹Server的method */
private void uploadFile(String path)
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设定传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary="+boundary);
/* 设定DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=\"uploadfile\";filename=\"" +
newName +"\"" + end);
ds.writeBytes(end); /* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(path);
/* 设定每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize]; int length = -1;
/* 从文件读取数据到缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将数据写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end); /* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}
/* 显示Dialog的method */
private void showDialog(String mess)
{
new AlertDialog.Builder(Pic.this).setTitle("Message")
.setMessage(mess)
.setNegativeButton("确定",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
} //class
}