我想显示从相机到图像视图拍摄的图片,但图像视图中的图片尺寸小,与从图库到图像视图显示的图片不同,图库中的图片尺寸适合屏幕,任何人能帮我解决这个问题吗?
这是我的java代码
Button btn_kamera = (Button) findViewById(R.id.btnKamera);
btn_kamera.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
}, 150);
}
});
Button btn_galeri = (Button) findViewById(R.id.btnGaleri);
btn_galeri.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
}, 150);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
this._image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
else if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK && null != data) {
this._image.setImageBitmap((Bitmap) data.getExtras().get("data"));
}
}
这是我的xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:text="Ambil Gambar"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:layout_marginBottom="30dp"
>
<com.indris.material.RippleView
android:id="@+id/btnKamera"
android:layout_weight="0.5"
android:layout_marginRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/card_bk"
android:gravity="center"
android:padding="15dp"
android:text="Kamera"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
ripple:alphaFactor="0.7"
ripple:hover="true"
ripple:rippleColor="#80D8FF" />
<com.indris.material.RippleView
android:id="@+id/btnGaleri"
android:layout_weight="0.5"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/card_bk"
android:gravity="center"
android:padding="15dp"
android:text="Galeri"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
ripple:alphaFactor="0.7"
ripple:hover="true"
ripple:rippleColor="#80D8FF" />
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Gambar Belum Dipilih"
android:visibility="visible"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_marginBottom="30dp" />
<com.indris.material.RippleView
android:id="@+id/btnProses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/editText1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginBottom="30dp"
android:background="@drawable/card_bk"
android:gravity="center"
android:padding="15dp"
android:text="Proses"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
ripple:alphaFactor="0.7"
ripple:hover="true"
ripple:rippleColor="#80D8FF" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:hint="Teks muncul di sini"
android:editable="false"
/>
</LinearLayout>
答案 0 :(得分:0)
我们无法正确判断您的代码。
你能发布完整的代码吗?
顺便说一句,你可以用androidhive的非常好的例子。检查here
为了防止较大图像的OutOfMemory异常,有BitmapFactory.Options
的功能。使用此图像时应对其进行采样。请查看以下代码段
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
imgPreview.setImageBitmap(bitmap);
这意味着您的图像大小分为第8部分。
最后,请确保您使用了options.inSampleSize
。如果是,请更改它,或者如果没有通过给定的链接。
答案 1 :(得分:0)
尝试以下代码: -
<强> take_image_activity.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<Button
android:id="@+id/buttonTakeImageActivity"
android:layout_width="170dp"
android:layout_height="60dp"
android:textSize="25sp"
android:text="Gallery"/>
<Button
android:id="@+id/buttonTakeImageCam"
android:layout_width="170dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:textSize="25sp"
android:text="Camera"/>
</LinearLayout>
</RelativeLayout>
<强> TakeImageActivity.java 强>
public class TakeImageActivity extends Activity implements OnClickListener {
File file,mFileTemp;
Button buttonTakeImage, buttonTakeImageCam;
ImageView imageView;
Uri mPhotoUri;
Bitmap thumbnail;
private Uri fileUri;
private static final String IMAGE_DIRECTORY_NAME = "PhotoEditor Camera";
public static final int MEDIA_TYPE_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.take_image_activity);
file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/FrameImages");
initialization();
}
private void initialization() {
// TODO Auto-generated method stub
buttonTakeImage = (Button) findViewById(R.id.buttonTakeImageActivity);
buttonTakeImageCam = (Button) findViewById(R.id.buttonTakeImageCam);
buttonTakeImage.setOnClickListener(this);
buttonTakeImageCam.setOnClickListener(this);
if (!isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesn't support camera",
Toast.LENGTH_LONG).show();
// will close the app if the device does't have camera
finish();
}
}
private boolean isDeviceSupportCamera() {
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.buttonTakeImageActivity:
if (Environment.getExternalStorageState().equals("mounted")) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(
Intent.createChooser(intent, "Select Picture:"),
1);
}
break;
case R.id.buttonTakeImageCam:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, 2);
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case 1:
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
Bitmap photo = getPreview(selectedImagePath);
yourimageview.setImageBitmap(photo);
break;
case 2:
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
break;
}
}
private void previewCapturedImage() {
try {
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 2;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
yourimageview.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public Bitmap getPreview(String fileName) {
File image = new File(fileName);
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getPath(), bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
return null;
}
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / 64;
// opts.inSampleSize = originalSize;
return BitmapFactory.decodeFile(image.getPath());
}
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);
}
}