单击上传按钮后,在另一个活动中显示图像

时间:2016-07-27 12:26:24

标签: android android-intent

我想在点击上传按钮后将我的图像显示给另一个活动。我有2个活动(Main和Main2)。目前,在从图库中选择图像后,我将uri用于下一个活动。我如何将uri放在我的上传按钮上呢?

MainActivity.Java

    private void uploadImage(){
        @Override
       public void onResponse(String s) {
           loading.dismiss();
           Toast.makeText(MainActivity.this, s , Toast.LENGTH_LONG).show();
           Intent i = new Intent(MainActivity.this,Main2Activity.class);
           startActivity(i);
           finish();
       }
   },


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
            Intent i = new Intent(this,Main2Activity.class);
            i.putExtra("imagepath", filePath.toString());
            startActivity(i);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4 个答案:

答案 0 :(得分:2)

您可以按照以下方式执行此操作:

定义一个类级变量:

   String filePathString = null;

private void uploadImage(){
 @Override
                public void onResponse(String s) {
                    loading.dismiss();
                    Toast.makeText(MainActivity.this, s , Toast.LENGTH_LONG).show();
                    Intent i = new Intent(MainActivity.this,Main2Activity.class);
                    startActivity(i);
                    finish();
                }
            },


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
            Intent i = new Intent(this,Main2Activity.class);
            filePathString = filePath.toString();
            i.putExtra("imagepath", filePath.toString());
            startActivity(i);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

然后在按钮上设置OnClickListener:

uploadButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
     Intent i = new Intent(this,Main2Activity.class);
     i.putExtra("imagepath", MainActivity.this.filePathString);
     startActivity(i);
    MainActivity.this.finish();
    }
});

答案 1 :(得分:1)

您可以将Uri变量更改为类级别变量,以存储从onActivityResult

获得的uri结果
private Uri filePath;

 if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);

            imageView.setImageBitmap(bitmap);
            Intent i = new Intent(this,Main2Activity.class);
            i.putExtra("imagepath", filePath.toString());
            startActivity(i);


        } catch (IOException e) {
            e.printStackTrace();
        }

然后在你的按钮中你可以将Uri传递给:

Intent i = new Intent(this,Main2Activity.class);
            i.putExtra("imagepath", filePath.toString());
            startActivity(i);

答案 2 :(得分:1)

试试这段代码 首先把uri作为意图中的字符串。

intent.putExtra("imageUri", imageUri.toString());

然后在第二个活动中转换回像这样的字符串

Uri myUri = Uri.parse(extras.getString("imageUri"));

答案 3 :(得分:0)

 Uri filePath = data.getData();
                String[] projection = {MediaStore.MediaColumns.DATA};
                Cursor cursor = managedQuery(filePath, projection, null, null,
                        null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();

                String selectedImagePath = cursor.getString(column_index);
    Then pass selectedImagePath in the intent like below
    Intent i = new Intent(this,Main2Activity.class);
            i.putExtra("imagepath", filePath.toString());
            startActivity(i);