android程序中
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT); 我是通过这种方式得到本机图片的路径的,现我用 System.out.println(intent.getData());
输出的路径为 content://media/external/images/media/227现问: 1 这是一种什么路径?
2 如何得到 /sdcard/image.JPG 这种形式的路径thanks
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT); 我是通过这种方式得到本机图片的路径的,现我用 System.out.println(intent.getData());
输出的路径为 content://media/external/images/media/227现问: 1 这是一种什么路径?
2 如何得到 /sdcard/image.JPG 这种形式的路径thanks
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);
}
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);
File tempfile = new File(mDirectoryname, sdf.format(new Date())
+ ".jpg");//tempfile为图片的路径
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);
cameraIntent.putExtra(imgurl, tempfile);
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)
{
//此处如何得到刚那图片的路径
}
}
}
现问:在 onActivityResult 方法中,如何得到刚那图片的路径
thanks
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
}
本人email [email protected]
谢谢大伙了