adrian
adrian

Reputation: 4594

problem with an imageview

I have an android app in which I take a photo using the android camera then I view the photo, if I like it then I upload the picture to a website.

Uploading the picture to the website I've noticed that there are a few pixels that are not visible on the phone!!!On the website the picture has a few extra-details that are not visible on the phone screen!!!

The picture on the phone screen is set in an imageview.

This is the layout of the activity:

<ImageView  
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_gravity="center"
android:scaleType="centerCrop"
android:id="@+id/myPic"
/>

<Button 
android:text="Confirm Photo"
android:layout_toLeftOf="@id/back"
android:id="@+id/confirm"
android:layout_marginTop="530dip"
android:layout_height="wrap_content" 
android:layout_width="165dip"
android:layout_alignParentRight="true"
>
   </Button>

 </RelativeLayout>

This is how I set the picture to the imageview:

Bundle extras = getIntent().getExtras();
    BitmapFactory.Options options=new BitmapFactory.Options();
    options.inSampleSize =2;
    byte[] imageData = extras.getByteArray("imageData");
    Bitmap myImage = BitmapFactory.decodeByteArray(imageData , 0, imageData.length,options);


    Matrix mat=new Matrix();
mat.postRotate(90);
bitmapResult = Bitmap.createBitmap(myImage, 0, 0,  myImage.getWidth(),myImage.getHeight(), mat, true); 

    Canvas c = new Canvas(bitmapResult);
    drawTextImage(bitmapResult);
    StoreByteImage(this, bitmapResult,100);
ImageView imageView = (ImageView) findViewById(R.id.myPic);
imageView.setImageBitmap(bitmapResult);

For extra code or other details I'm here to give it to you.Thanks

Upvotes: 2

Views: 5640

Answers (4)

NSjonas
NSjonas

Reputation: 12032

If you're talking about the image on the website being larger than the one on your phone, then it's because your scale type is set to:

android:scaleType="centerCrop"

and your imageview bound probably doesn't match the image (thus the image is cropped). Try adding this line to the imageview and see if it makes a difference.

android:adjustViewBounds="true"

Lastly change you layout height and width attributes to:

layout_width="fill_parent"
layout_height="wrap_content"

Upvotes: 7

juell
juell

Reputation: 4960

Sounds like you want scaleType "centerInside" instead of "centerCrop".

Upvotes: -1

hooked82
hooked82

Reputation: 6376

If you image size is outside of the bounds of your ImageView when using the scaleType attribute:

android:scaleType="centerCrop"

Then your image will be cropped and only show what fits into your ImageView. To work around this, here's a helper method that you can call to scale your image down to the size that you need. It may need some tweaking on your end to fit your situation, but it should be helpful. It's a slightly modified version found at Strange out of memory issue while loading an image to a Bitmap object:

public static Bitmap decodeFile(File f, int maxSize){
    if (f == null)
        return null;
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<maxSize || height_tmp/2<maxSize)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

Upvotes: 2

Shlublu
Shlublu

Reputation: 11027

According to the comments below your question, this is caused by the android:scaleType="centerCrop" statement. It hides the border of your image as the only part displayed on the device's screen is the part that fits it.

NSJonas gave more detail in this page about how to make the image to fit the screen.

Upvotes: 1

Related Questions