获取imageview资源的文件路径并上传到服务器


Get filepath of imageview resource and upload to server

在我的应用程序中,用户可以上传他们的个人资料照片,它会将图像视图更改为他们的照片。我需要将文件路径存储在一个变量中。这怎么可能呢?

下面是代码:
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, 1);

然后结果将profile 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);
    }
}

在apiconconnector类中:

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 -因为它是值得的。

如果有任何帮助,我将不胜感激。

存储文件路径与存储其他任何数据相同。你可以使用共享偏好设置,或者将其存储在手机上的数据库中。以下是所有选项: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");