简体   繁体   English

在imageView中设置从图库中选取的图像

[英]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. 从图库/相机中选择图像后,我想在imageView中设置图像。 I am facing two problems 1. only the low quality pictures are setting in the imageView, not the one of camera quality. 我面临两个问题:1.在imageView中仅设置了低质量的图片,而不是相机质量中的一种。
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). 2.设置“低质量图像”后,将设备倾斜,imageView将变为空白(就像在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á 1-从ImageView获取位图2-获取位图的尺寸3-计算缩放倍数4-缩放5-获取缩放后的位图尺寸6-应用缩放后的图像7-将ImageView调整为缩放后的位图的精确尺寸8-Voilá

here is the code and use it To fit The Image Clicked from the camera the the ImageView 这是代码,并使用它来适合图像单击相机中的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
}       

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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