简体   繁体   中英

Set an image picked from gallery in an imageView

I want to set the image in an imageView after choosing it from the gallery/camera. I am facing two problems 1. only the low quality pictures are setting in the imageView, not the one of camera quality.
2. After setting the The low quality image when the device is tilt the imageView become blank(like before the image was set in imageView).

package com.example.faizantahir.naughtyfire;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class PicUpload extends ActionBarActivity {
    Button button,button1;
    ImageView imageview;
    String selectedImagePath;
    private static final int SELECT_PICTURE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pic_upload);
    button=(Button)findViewById(R.id.button5);
    button1=(Button)findViewById(R.id.button6);
    imageview=(ImageView)findViewById(R.id.imageView);
    button1.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View view){


                    Intent intent3=new Intent();
                    intent3.setClass(PicUpload.this,FirstFragment.class);
                    startActivity(intent3);
                }
            }

    );



    button.setOnClickListener(
        new Button.OnClickListener(){
            public void onClick(View view){
                Toast t= Toast.makeText(PicUpload.this,"Yoaklfhlkas",Toast.LENGTH_LONG);
                t.show();
                CustomDialogClass cdd = new CustomDialogClass(PicUpload.this);
                cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                cdd.show();

            }

    }

    );
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch(requestCode) {
        case 1:
                if(resultCode == RESULT_OK){
                    Uri selectedImageUri = imageReturnedIntent.getData();
                    selectedImagePath = getPath(selectedImageUri);
                    System.out.println("Image Path : " + selectedImagePath);
                    imageview.setImageURI(selectedImageUri);
                }
            break;
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}

you can use one library for faster image loading, it cache image with quality you ask for.

Universal-Image-Loader

it's easy to implement as snippet given in page.

I took a simple approach as those are usually the best and fastest:

1- Get bitmap from the ImageView 2- Get bitmap's dimensions 3- Calculate scaling multiplier 4- Scale 5- Get scaled bitmap dimensions 6- Apply the scaled image 7- Resize ImageView to exact dimensions of the scaled bitmap 8- Voilá

here is the code and use it To fit The Image Clicked from the camera the the ImageView

private void scaleImage(ImageView view, int boundBoxInDp)

{

Drawable drawing = view.getDrawable();

Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

// Get current dimensions
int width = bitmap.getWidth();
int height = bitmap.getHeight();

// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) boundBoxInDp) / width;
float yScale = ((float) boundBoxInDp) / height;
float scale = (xScale <= yScale) ? xScale : yScale;

// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);

// Create a new bitmap and convert it to a format understood by the ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
width = scaledBitmap.getWidth();
height = scaledBitmap.getHeight();

// Apply the scaled bitmap
view.setImageDrawable(result);

// Now change ImageView's dimensions to match the scaled image
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);

}

private int dpToPx(int dp)
{
    float density = getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}

And To use Use this

public class TestActivity extends Activity

{

    @Override public void onCreate(Bundle savedInstanceState)
    {


 super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    // ImageViews must be under LinearLayout in the xml or the code crashes into scaleImage(). Tune scaleImage() into your needs.
    ImageView view1 = (ImageView) findViewById(R.id.test1);
    ImageView view2 = (ImageView) findViewById(R.id.test2);

    scaleImage(view1, 250); // in dp
    scaleImage(view2, 100); // in dp
}       

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM