我想从图库/相机中选择后,在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);
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
我采用了一种简单的方法,因为它们通常是最好的和最快的:
1-从ImageView
获取位图
2-获取位图的尺寸
3-计算缩放倍数
4-规模
5-获取缩放的位图尺寸
6-应用缩放图像
7-将ImageView
的大小调整为缩放位图的精确尺寸
8-Voilá
这里是代码并使用它来适应图像从相机中点击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);
}
并使用“使用此
”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
}
}