如何在第一个活动和另一个活动中显示所选的图库图片?

时间:2017-05-20 15:36:23

标签: android

public class Main2Activity extends AppCompatActivity {

    private static int PICK_IMAGE_REQUEST = 1;
    ImageView imgView;
    static final String TAG = "Main2Activity";
    public String[] filePathColon;

    @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) {

                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);
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                final String filePath = cursor.getString(columnIndex);

                imgView = (ImageView) findViewById(imageView);
                imgView.setImageBitmap(scaled);

                Button button3 = (Button) findViewById(R.id.button3);
                button3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getApplicationContext(), Main3Activity.class);
                        intent.putExtra("imageUri", filePath);
                        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();

        }
    }
}


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);

        Bitmap ba = this.getIntent().getParcelableExtra("imageUri");
        imageView.setImageBitmap(ba);
    }
}

有什么问题?

1 个答案:

答案 0 :(得分:0)

步骤1:删除这些行:

        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        final String filePath = cursor.getString(columnIndex);

第2步:将intent.putExtra("imageUri", filePath);替换为intent.setData(uri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

步骤3:替换:

Bitmap ba = this.getIntent().getParcelableExtra("imageUri");
imageView.setImageBitmap(ba);

使用:

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

最后,使用图片加载库(例如Picasso)替换您的位图工作(第一个活动中未使用的Bitmap和第二个活动中的setImageURI())您可以将所有这些数据处理从主应用程序线程中移除。