Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在imageView中设置从库中拾取的图像_Java_Android_Image_Import - Fatal编程技术网

Java 在imageView中设置从库中拾取的图像

Java 在imageView中设置从库中拾取的图像,java,android,image,import,Java,Android,Image,Import,从gallery/camera中选择图像后,我想在imageView中设置图像。我面临两个问题 1.在imageView中仅设置低质量的图片,而不是相机质量的图片。 2.设置设备倾斜时的低质量图像后,imageView将变为空白,就像在imageView中设置图像之前一样 package com.example.faizantahir.naughtyfire; import android.content.Intent; import android.database.Cursor; impor

从gallery/camera中选择图像后,我想在imageView中设置图像。我面临两个问题 1.在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);
}

}您可以使用一个库来更快地加载图像, 它会以您要求的质量缓存图像


按照第页中给出的代码片段很容易实现。

我采用了一种简单的方法,因为这些方法通常是最好、最快的:

1-从ImageView获取位图 2-获取位图的尺寸 3-计算缩放倍增器 4级 5-获取缩放位图尺寸 6-应用缩放图像 7-将ImageView调整为缩放位图的精确尺寸 8-瞧

这是代码,使用它来匹配从相机单击的图像

{

使用这个

public class TestActivity extends Activity
{

}

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);
}
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
}