0

I have a simple Android application. I want to start the camera by creating an intent and then take photo and set the photo in an imageview which is dynamically added to the relative layout. The relative layout is static inside a layout.

I have been trying the following code, but it doesn't work. If I debug I can see the image taken from the camerai in the imageview.

public void setImage(Uri imgUri){
        try{

            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            int width = metrics.widthPixels;
            int height = metrics.heightPixels;

            ImageView imgView = new ImageView(this);

            imgView.setImageURI(imgUri);
            imgView.setBackgroundColor(Color.BLACK);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            imgView.setLayoutParams(params);

            imgHolder.addView(imgView);


            Button btnTest = new Button(this);
            btnTest.setText("blabla");;

            imgHolder.addView(btnTest);
            Toast.makeText(getApplicationContext(),"Picture will be shown now", Toast.LENGTH_LONG).show();

        }catch(Exception e){
            Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
        }
    }

P.S. I can take the photo and save it to the picture folder (there is no R.drawable.image because i create it dynamically by creating an Uri to that file. The problem is set the imageview from the uri. There are 4 ways to set the image to the imageview. I already tried all of them. When i debug right after setting the image i can see the bitmap of the imageview and it is the picture i took by the camera.

AngularChan
  • 135
  • 2
  • 11
  • Possible duplicate of [ImageView.setImageURI does NOT work when trying to assign a R.drawable.X Uri](https://stackoverflow.com/questions/2313148/imageview-setimageuri-does-not-work-when-trying-to-assign-a-r-drawable-x-uri) – MSpeed Jun 13 '18 at 12:50

3 Answers3

0

try to call this at the end:

imgHolder.invalidate();

Just in case, make sure imgHolder is visible.

giroxiii
  • 655
  • 5
  • 14
  • Hi, thanks for your answer. The imgHolder is visible and the button is added. The imageview also has a background color, but the image itself isn't being shown. – AngularChan Jun 13 '18 at 12:51
0

Try this:

RelativeLayout.LayoutParams imParams = 
    new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    ImageView imLove = new ImageView(context);
    imLove.setImageResource(getmyImage());
    imLove.setId(2);
    addView(imLove,imParams);
0

First, you need to declare an Intent that retrieves a photo taken by the Camera. Don't forget the permissions for the application. For example, this code works well for such an action :

PackageManager pm = v.getContext().getPackageManager();

if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    if (ContextCompat.checkSelfPermission(v.getContext(), Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
        // Permission is not granted
        Log.d("No permission", "You need to enable the CAMERA permissions to access to this feature");
        ActivityCompat.requestPermissions(thisActivity,
                            new String[]{Manifest.permission.CAMERA},REQUEST_IMAGE_CAPTURE);
    }
    else{
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}
else{
    Snackbar.make(v,"No CAMERA : Your phone doesn't have the correct feature for this action", 1000);
}

Then, you need to get the result from the intent by overriding the method onActivityResult() :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        photo = (Bitmap) extras.get("data");
        imageView.setImageBitmap(photo);
    }
}
  • Hi there, i already have the feature for taking photo from camera. The photo is saved in the picture folder. In my code after i set the imageview with the Uri i can watch in the image by evaluating the image view one step after setting the image. I also tried your code, but inside the onActivityResult the data is null. – AngularChan Jun 13 '18 at 13:36