Android:获取我们在图库中看到的图像而不是 Stratch 图像
在我的应用程序中,我使用此代码从图库和相机获取图像:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
//Bitmap croppedImage = BitmapFactory.decodeFile(imagePath);
tempBitmap = BitmapFactory.decodeFile(imagePath);
if(!(tempBitmap == null))
{
// here i am changing display to the tempBitmap
photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
//photoBitmap = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight());
takePhotoFromGallery = true;// edited
}
else
Toast.makeText(getApplicationContext(), "Image is not valid", Toast.LENGTH_SHORT).show();
}
if(resultCode == RESULT_OK && requestCode==TAKE_PHOTO_CODE){
final File file = getTempFile(this);
try {
tempBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file));
photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
takePhotoFromCamera = true;
// do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在,一切正常。但我得到的图像是Stratch。
我认为这是因为这一行:
photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
这里我用显示器的宽度、高度来拉伸图像的宽度、高度。
我想要的是图像应该是 Dispaly,因为我们通常可以在图库中显示图像。那么如何才能实现呢???
这是我从相机拍摄的图像:
现在这是我在应用程序中看到的内容:
现在第二张图像几乎没有了。因为那行代码。
那么我应该怎么做才能使其高度和宽度正常? 谢谢。 谢谢。
In my app i am using this code to get Image from Gallery and Camera:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
//Bitmap croppedImage = BitmapFactory.decodeFile(imagePath);
tempBitmap = BitmapFactory.decodeFile(imagePath);
if(!(tempBitmap == null))
{
// here i am changing display to the tempBitmap
photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
//photoBitmap = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight());
takePhotoFromGallery = true;// edited
}
else
Toast.makeText(getApplicationContext(), "Image is not valid", Toast.LENGTH_SHORT).show();
}
if(resultCode == RESULT_OK && requestCode==TAKE_PHOTO_CODE){
final File file = getTempFile(this);
try {
tempBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file));
photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
takePhotoFromCamera = true;
// do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now, all works fine. But i got Image as Stratch.
I thaught it is because this line:
photoBitmap = Bitmap.createScaledBitmap(tempBitmap, display.getWidth(), display.getHeight(), true);
here i am stratching the image width,height with display's width,height.
I want is the Image should be Dispaly as we can normaly show thew image in gallery. So how to make it possible ???
This is the image which i am snaping from the Camera:
And Now this is What i see in my Application:
Now Second image got little stratch. as because of that line code.
So what should i have to do to make is normal in its height and width ??
Thanks.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ImageView 并将图像视图的缩放类型设置为 Center-Inside。
Use ImageView and set scale type of image view to Center-Inside.