将捕获的图像或从图库传递到另一个活动

时间:2018-11-17 17:00:26

标签: android android-intent bitmap uri gallery

我一直试图将图像从gallerycamera传递到另一个活动,但是它们不能正常工作。如果图库中的图像有效,则相机中的图像无效,反之亦然。请帮助我修复代码。这是我的MainActivity和SecondActivity代码。谢谢!

MainActivity.java

public class MainActivity extends AppCompatActivity {

Button btnStart,btnGallery,btnAbout;
ImageView reademlogo, pianotiles,imgclouds,imgclouds2,imgsharp,imgfclef;
TextView txtread;
Typeface tf1;
private final int REQUEST_IMAGE_CAPTURE = 1;
private final int PICK_IMAGE = 1;
Uri imageUri;

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

    UserInterface();
}

 private void UserInterface() {
    btnStart = (Button)findViewById(R.id.btnStart);
    btnGallery = (Button)findViewById(R.id.btnGallery);
    btnAbout = (Button)findViewById(R.id.btnAbout);
    reademlogo = (ImageView)findViewById(R.id.reademLogo);
    pianotiles = (ImageView)findViewById(R.id.pianotiles);
    imgclouds = (ImageView)findViewById(R.id.imgclouds);
    imgclouds2 = (ImageView)findViewById(R.id.imgclouds2);
    imgsharp = (ImageView)findViewById(R.id.imgsharp);
    imgfclef = (ImageView)findViewById(R.id.imgfclef);

    txtread = (TextView)findViewById(R.id.txtread);

    tf1 = Typeface.createFromAsset(getAssets(),
          "fonts/VeganStylPersonalUse.ttf");
    txtread.setTypeface(tf1);
 }

 public void captureImage(View view) {
    Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (iCamera.resolveActivity(getPackageManager()) != null) {
         startActivityForResult(iCamera, REQUEST_IMAGE_CAPTURE);
     }
 }

 public void galImage(View view) {
    Intent iGallery = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    iGallery.setType("image/*");
    startActivityForResult(iGallery, PICK_IMAGE);
 }

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

    if(resultCode == RESULT_OK && requestCode == REQUEST_IMAGE_CAPTURE){

        Bitmap bitmap = (Bitmap)Objects.requireNonNull(data.getExtras()).get("data");
        Intent intent = new Intent(MainActivity.this,Camera.class);
        intent.putExtra("Bitmap",bitmap);
        startActivity(intent);
    }
    else if(requestCode == PICK_IMAGE){
        if(data != null){
            imageUri = data.getData();
            Intent intent = new Intent(MainActivity.this,Camera.class);
            intent.putExtra("imageUri",imageUri);
            startActivity(intent);
        }
     }
   }
}

Camera.java

public class Camera extends AppCompatActivity {

Button btnCap, btnUse;
ImageView imageView3;
Uri imageUri;



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

    btnCap = (Button) findViewById(R.id.btnCap);
    btnUse = (Button) findViewById(R.id.btnUse);
    imageView3 = (ImageView) findViewById(R.id.imageView3);

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

    Intent intentCam = getIntent();
    Bitmap camera_img = (Bitmap)intentCam.getParcelableExtra("Bitmap");
    if(camera_img != null){
        imageView3.setImageBitmap(camera_img);
     } 
  }
}

1 个答案:

答案 0 :(得分:1)

尝试一下,对我来说很好。

 //Use  this method to select image from Gallery
 private void processGalleryImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),
            GALLERY_REQUEST_CODE);
}

//Use  this method to click image from Camera
private void processCameraImage() {
    Intent cameraIntent = new 
    Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}



 //Use  this method to get image from Gallery/Captured from Camera
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAMERA_REQUEST_CODE) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");

            //Starting activity (ImageViewActivity in my code) to preview image 
            Intent intent = new Intent(this, ImageViewActivity.class);
            intent.putExtra("BitmapImage", photo);
            startActivity(intent);
        } else if (requestCode == GALLERY_REQUEST_CODE) {
            if (data.getData() != null) {
                Uri imageUri = data.getData();

                //Starting activity (ImageViewActivity in my code) to preview image 
                Intent intent = new Intent(this, ImageViewActivity.class);
                intent.putExtra("ImageUri", imageUri.toString());
                startActivity(intent);
            }
        }
    }
}


 //ImageViewActivity code

 private Bitmap bitmap;
 private String uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_view);
    ButterKnife.bind(this);


    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        bitmap = bundle.getParcelable("BitmapImage");
        uri = bundle.getString("ImageUri");

        if (bitmap != null)
            imageView.setImageBitmap(bitmap);
        else
            Glide.with(this).load(uri).into(imageView);
    }
}

如果有图像URL,我正在使用Glide库在图像视图中加载图像。