将图库图像从MainActivity传递到另一个Activity

时间:2017-05-21 03:21:03

标签: android

我的Main2Activity班级

public class Main2Activity extends AppCompatActivity {

    private static int PICK_IMAGE_REQUEST = 1;
    ImageView imgView;
    static final String TAG = "Main2Activity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }

    public void loadImagefromGallery(View view) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

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

            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data) {

                final Uri uri = data.getData();

                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                int nh = (int) (bitmap.getHeight() * (1024.0 / bitmap.getWidth()));
                Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 1024, nh, true);
                imgView = (ImageView) findViewById(imageView);
                imgView.setImageBitmap(scaled);

                Button button3 = (Button) findViewById(bt_tab3);
                button3.setOnClickListener
                        (new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getApplicationContext(), Main3Activity.class);
                        intent.setData(uri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        startActivity(intent);
                    }
                });
            } else {
                Toast.makeText(this, "No.", Toast.LENGTH_LONG).show();
            }

        } catch (Exception e) {
            Toast.makeText(this, "Oops! Sorry", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}

Main3Activity

public class Main3Activity extends AppCompatActivity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        imageView = (ImageView) findViewById(R.id.imageView2);
    }

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

        Uri imageUri = getIntent().getData();
        imageView.setImageURI(imageUri);
    }
}

如何在MainActivity和另一个Activity中显示相同的图库图片?

3 个答案:

答案 0 :(得分:0)

将来自uri的文件路径作为$ cat file1 #export PATH="$HOME/.rbenv/bin:$PATH" #eval "$(rbenv init -)" $ cat file2 This is what I'm trying to uncomment: #export PATH="$HOME/.rbenv/bin:$PATH" #eval "$(rbenv init -)" So I did my research and found this command that does what I'm looking for 传递给$ awk 'NR==FNR{a[$0];next} $0 in a{sub(/./,"")} 1' file1 file2 This is what I'm trying to uncomment: export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)" So I did my research and found this command that does what I'm looking for

$ awk 'NR==FNR{a[$0];next} {for (i in a) if (s=index($0,i)) $0=substr($0,1,s-1) substr($0,s+1)} 1' file1 file2
This is what I'm trying to uncomment:

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

So I did my research and found this command that does what I'm looking for

并在String中获取从调用Main3Activity传递的数据。

button3.setOnClickListener
        (new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(), Main3Activity.class);
        intent.putExtra("filePath", uri.getPath());
        startActivity(intent);
    }
});

您需要在Main3Activity

中添加以下权限
Activity

答案 1 :(得分:0)

基于您不接受以前的答案,这是一种不同的方法:

首先将图像转换为字节数组,如:

Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is bitmap object   
byte[] b = baos.toByteArray(); 

然后将其转换为字符串,如:

String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

然后从你的第一个活动中产生一个意图并将该字符串作为额外的:

Intent imageIntent = new Intent(context,Main3Activity.class);
imageIntent.putExtra("image",encodedImage);
startActivity(imageIntent);

之后,使用getIntent然后getExtra

在下一个活动中轻松获取此字符串

希望这会有所帮助。

答案 2 :(得分:0)

将您的Button相关代码移至onCreate(),并使用意图UriString发送为extras

更新Main2Activity,如下所示:

public class Main2Activity extends AppCompatActivity {

    static final String TAG = "Main2Activity";

    private static int PICK_IMAGE_REQUEST = 1;

    ImageView imgView;
    Button button3;

    Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        imgView = (ImageView) findViewById(R.id.imageView);
        button3 = (Button) findViewById(R.id.bt_tab3);

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), Main3Activity.class); 
                intent.putExtra("imageUri", imageUri.toString());
                startActivity(intent);
            }
        });
    }

    public void loadImagefromGallery(View view) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }

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

            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data) {
                // Get uri
                imageUri = data.getData();

                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                int nh = (int) (bitmap.getHeight() * (1024.0 / bitmap.getWidth()));
                Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 1024, nh, true);
                // Set image
                imgView.setImageBitmap(scaled);

            } else {
                Toast.makeText(this, "No.", Toast.LENGTH_LONG).show();
            }

        } catch (Exception e) {
            Toast.makeText(this, "Oops! Sorry", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}

Uri获取string intent,并使用Uri方法从string构建Uri.parse()

更新Main3Activity,如下所示:

public class Main3Activity extends AppCompatActivity {
    ImageView imageView;

    Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        imageView = (ImageView) findViewById(R.id.imageView2);

        if (getIntent().getExtras() != null) {
            imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
            imageView.setImageURI(imageUri);
        }
    }
}