在我的应用程序中,用户可以上传他们的个人资料照片,并将图像视图更改为他们的照片。我需要将文件路径存储在变量中。这怎么可能?
这是代码:
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
然后结果会将配置文件IMAGE VIEW从手机更改为所选图像。
@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 - 它的价值。
对此的任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
存储文件路径与存储任何其他数据相同。您可以使用共享首选项,也可以将其存储在手机上的数据库中。以下是所有选项:http://developer.android.com/guide/topics/data/data-storage.html
如果您尝试将文件路径传递给第二个活动,请使用extras
,如下所示:
filePath = "my/file/path.jpg"
Intent i = new Intent(this, ToClass.class);
i.putExtra("filePath", filePath);
startActivity(i);
然后在第二项活动中:
Intent intent = getIntent();
String easyPuzzle = intent.getExtras().getString("filePath");