【问题标题】:Get filepath of imageview resource and upload to server获取imageview资源的文件路径并上传到服务器
【发布时间】:2014-07-15 11:35:03
【问题描述】:

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

代码如下:

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 - 物有所值。

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: php android postgresql


    【解决方案1】:

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

    【讨论】:

    • 我需要将整个图像传递到下一个活动并上传它,是否可以将文件路径存储在共享首选项中并尝试在第二个活动中上传图像?
    • @user3241507 如果您尝试将文件路径传递给第二个活动,那么您可以简单地使用意图。见编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2013-07-09
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多