到上传到服务器的文件路径 [英] get the file path to upload to server
本文介绍了到上传到服务器的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建中,我要发送图像,我现在用的摄像头和画廊intent.Now我想是要获取的用户选择,这样我可以上传图片的路径网络Server.For一个应用程序到server.Now我如何能得到路径???
code
如果(userSelection.equals(库)){
意图=新意图(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(意向,0);
btChooseDoc.setText(选择文件);
} 如果(userSelection.equals(照相机)){
意图=新意图(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(意向,1);
btChooseDoc.setText(选择文件);
} 如果(userSelection.equals(其他)){
意图=新意图(Intent.ACTION_GET_CONTENT);
intent.setType(文件/ *);
startActivityForResult(意向,2);
btChooseDoc.setText(选择文件);
}
}公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据);//位图摄像头;
ByteArrayOutputStream字节=新ByteArrayOutputStream(); 如果(要求code == 1安培;&安培;结果code == getActivity()RESULT_OK){ 。摄像头=(位图)data.getExtras()获得(数据);
camera.com preSS(Bitmap.Com pressFormat.JPEG,60字节);
galleryImage = Bitmap.createScaledBitmap(galleryImage,150,150,FALSE);
image.setImageBitmap(摄像头);
}
如果(要求code == 0安培;&安培;结果code == getActivity()RESULT_OK){ galleryUri = data.getData();
尝试{
galleryImage =去codeURI(getActivity(),galleryUri);
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}
galleryImage.com preSS(Bitmap.Com pressFormat.JPEG,60字节);
galleryImage = Bitmap.createScaledBitmap(galleryImage,150,150,FALSE);
image.setImageBitmap(galleryImage);
} }
解决方案
您好:你可以试试下面code从画廊获得所选图像的路径。
要打开多媒体资料...
私有静态诠释RESULT_LOAD_IMAGE = 1; //变量全局声明
在 ButtonClickListener()
意向书I =新意图(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(ⅰ,RESULT_LOAD_IMAGE);
活动结果:
如果(数据!= NULL){ 乌里selectedImage = data.getData();
的String [] = filePathColumn {MediaStore.Images.Media.DATA};
光标光标= context.getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL); cursor.moveToFirst();
INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
字符串picturePath = cursor.getString(参数:columnIndex); //图像的路径... cursor.close();
}
I am creating a application in which i want to send image to the web Server.For that i am using camera and gallery intent.Now what i want is to get the path of the image which user selects so that i can upload to the server.Now how can i get the path???
Code
if (userSelection.equals("Gallery")) {
intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
btChooseDoc.setText("Choose File");
}
if (userSelection.equals("Camera")) {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
btChooseDoc.setText("Choose File");
}
if (userSelection.equals("Other")) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, 2);
btChooseDoc.setText("Choose File");
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Bitmap camera;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
if (requestCode == 1 && resultCode == getActivity().RESULT_OK) {
camera = (Bitmap) data.getExtras().get("data");
camera.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
galleryImage = Bitmap.createScaledBitmap(galleryImage, 150, 150, false);
image.setImageBitmap(camera);
}
if (requestCode == 0 && resultCode == getActivity().RESULT_OK) {
galleryUri = data.getData();
try {
galleryImage = decodeUri(getActivity(), galleryUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
galleryImage.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
galleryImage = Bitmap.createScaledBitmap(galleryImage, 150, 150, false);
image.setImageBitmap(galleryImage);
}
}
解决方案
Hello You Can Try Below code to get path of selected Image from gallery..
To Open Gallery...
private static int RESULT_LOAD_IMAGE = 1; //variable declared globally
on ButtonClickListener()
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Activity Result :
if(data != null){
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex); //path of image...
cursor.close();
}
这篇关于到上传到服务器的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文