Php 获取imageview资源的文件路径并上载到服务器
在我的应用程序中,用户可以上传他们的个人资料照片,并将图像视图更改为他们的照片。我需要将文件路径存储在变量中。这怎么可能 代码如下:Php 获取imageview资源的文件路径并上载到服务器,php,android,postgresql,Php,Android,Postgresql,在我的应用程序中,用户可以上传他们的个人资料照片,并将图像视图更改为他们的照片。我需要将文件路径存储在变量中。这怎么可能 代码如下: Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, 1); 然后,结果会将个人资料图像视图更
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
然后,结果会将个人资料图像视图更改为从手机中选择的图像
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri bitmapUri = data.getData();
try {
Bitmap b = Media.getBitmap(getContentResolver(), bitmapUri);
profile.setImageBitmap(b);
submit.setEnabled(true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在此之后,我需要将其发送到我的ApiConnector类与数据库连接-通过PHP运行以访问数据库并将其上载到服务器
以下是到目前为止要上传到服务器的代码,其中一个参数是图像:
在上载类中:
private class SignUpForProfile extends AsyncTask<ApiConnector, Long, JSONArray> {
User user = User.getInstance();
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].signupForProfile(user.getId(), firstname.getText().toString(), lastname.getText().toString(), age.getText().toString(), profileURL);
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
done(jsonArray);
}
}
在ApiConnector类中:
public JSONArray signupForProfile(String id, String firstname, String lastname, String age, String pic) {
HttpEntity httpEntity = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
nameValuePairs.add(new BasicNameValuePair("firstname",firstname));
nameValuePairs.add(new BasicNameValuePair("lastname",lastname));
nameValuePairs.add(new BasicNameValuePair("age",age));
nameValuePairs.add(new BasicNameValuePair("pic_large",pic));
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL + "create_profile.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
} catch (IOException e) {
e.printStackTrace();
}
JSONArray jsonArray = null;
if(httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
我正在使用Postgresql——因为它是值得的
非常感谢您的帮助。存储文件路径与存储任何其他数据相同。您可以使用共享首选项,也可以将其存储在手机的数据库中。以下是所有选项: 如果您试图将文件路径传递到第二个活动,只需使用如下附加功能: 然后在第二个活动中,您要执行以下操作:
Intent intent = getIntent();
String easyPuzzle = intent.getExtras().getString("filePath");
我需要将整个图像传递到下一个活动并将其上载,是否可以只将文件路径存储在共享pref中,然后尝试在第二个活动中上载图像?@user3241507如果您试图将文件路径传递到第二个活动,则可以简单地使用intents。参见编辑
Intent intent = getIntent();
String easyPuzzle = intent.getExtras().getString("filePath");