英文:
How to show images with a high resolution in Android Studio?
问题
以下是翻译好的部分:
我正在尝试将从图库中选择的图像显示到我的图像视图中。但只有在选择像素较低的图像时才能显示,在选择像素较高的图像时会显示以下错误。
W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
如何才能自动调整所选图像的大小/分辨率,以适应设备的限制。
以下是我的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
try {
Bitmap bitmap = null;
if (android.os.Build.VERSION.SDK_INT >= 29) {
ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), selectedImage);
bitmap = ImageDecoder.decodeBitmap(source);
}
else{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
}
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
以下是我的图像视图的 XML 代码:
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_foreground" />
英文:
I am trying to show the selected image from gallery to my Image View. But it only shows the images with low pixels on selecting images with high pixels it is showing following error.
W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)
How can I automatically adjust the size/resolution of the selected image to fit according to devices limit.
Here is my code:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
try {
Bitmap bitmap = null;
if (android.os.Build.VERSION.SDK_INT >= 29) {
ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), selectedImage);
bitmap = ImageDecoder.decodeBitmap(source);
}
else{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
}
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is my xml code for Image View:
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_foreground" />
答案1
得分: 0
我使用Glide找到了一个解决方案。
首先按照以下步骤安装Glide:
步骤1:在你的build.gradle(YourAppName)中添加以下代码:
allprojects {
repositories {
//其他仓库
mavenCentral()
maven { url 'https://maven.google.com' }
}
}
步骤2:在你的build.gradle(Module:app)中添加以下代码:
dependencies {
//其他依赖
implementation 'com.github.bumptech.glide:glide:4.11.0'
}
现在同步你的项目。然后你只需要获取图片的Uri。调整后的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
ImageView imageView = findViewById(R.id.imageView);
try{
Glide.with(this)
.load(selectedImage)
.into(imageView);
} catch (IOException e) {
e.printStackTrace();
}
}
}
在XML中添加这一行:
android:scaleType="fitCenter"
英文:
I found a solution using glide.
First install glide using following steps:
Step 1: In you build.gradle(YourAppName) add these lines
allprojects {
repositories {
//Other repositories
mavenCentral()
maven { url 'https://maven.google.com' }
}
}
Step 2: In your build.gradle(Module:app) add these lines
dependencies {
// other dependencies
implementation 'com.github.bumptech.glide:glide:4.11.0'
}
Now sync your project. Then you only need to get Uri of the image. Adjusted code:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
ImageView imageView = findViewById(R.id.imageView);
try{
Glide.with(this)
.load(selectedImage)
.into(imageView);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In XML add this line:
android:scaleType="fitCenter"
答案2
得分: 0
您的错误消息已经说明了问题,您的图像大小太大。以下是调整大小的示例代码:
public Bitmap resizeBitmap(Bitmap source, int width, int height) {
if (source.getHeight() == height && source.getWidth() == width) return source;
int maxLength = Math.min(width, height);
try {
source = source.copy(source.getConfig(), true);
if (source.getHeight() <= source.getWidth()) {
if (source.getHeight() <= maxLength) { // 如果图像已经小于所需高度
return source;
}
double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
int targetWidth = (int) (maxLength * aspectRatio);
return Bitmap.createScaledBitmap(source, targetWidth, maxLength, false);
} else {
if (source.getWidth() <= maxLength) { // 如果图像已经小于所需宽度
return source;
}
double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
int targetHeight = (int) (maxLength * aspectRatio);
return Bitmap.createScaledBitmap(source, maxLength, targetHeight, false);
}
} catch (Exception e) {
return source;
}
}
英文:
Your error message says it all, the size of your image is too large. Here's something to resize it:
public Bitmap resizeBitmap(Bitmap source, int width,int height) {
if(source.getHeight() == height && source.getWidth() == width) return source;
int maxLength=Math.min(width,height);
try {
source=source.copy(source.getConfig(),true);
if (source.getHeight() <= source.getWidth()) {
if (source.getHeight() <= maxLength) { // if image already smaller than the required height
return source;
}
double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
int targetWidth = (int) (maxLength * aspectRatio);
return Bitmap.createScaledBitmap(source, targetWidth, maxLength, false);
} else {
if (source.getWidth() <= maxLength) { // if image already smaller than the required height
return source;
}
double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
int targetHeight = (int) (maxLength * aspectRatio);
return Bitmap.createScaledBitmap(source, maxLength, targetHeight, false);
}
}
catch (Exception e)
{
return source;
}
}
答案3
得分: 0
我想出来的处理大位图的解决方案是这样的。这个方法的工作原理是,你的代码将调用第二个方法,该方法将调用第一个方法,然后你就可以获得一个适合你设备缩放的位图。
public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight)
{
// 读取磁盘上图像的尺寸
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1;
if (srcHeight > destHeight || srcWidth > destWidth)
{
if (srcWidth > srcHeight)
{
inSampleSize = Math.round(srcHeight / destHeight);
} else
{
inSampleSize = Math.round(srcWidth / destWidth);
}
}
options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeFile(path, options);
}
public static Bitmap getScaledBitmap(String path, Activity activity)
{
android.graphics.Point size = new android.graphics.Point();
activity.getWindowManager().getDefaultDisplay().getSize(size);
return getScaledBitmap(path, size.x, size.y);
}
英文:
A solution I came up with to work with large bitmaps was this. The way this works is your code will call the second method, which will call the first method, and then you've got a bitmap that'll be scaled for your device.
public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight)
{
// read in the dimensions of the image on disk
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1;
if (srcHeight > destHeight || srcWidth > destWidth)
{
if (srcWidth > srcHeight)
{
inSampleSize = Math.round(srcHeight / destHeight);
} else
{
inSampleSize = Math.round(srcWidth / destWidth);
}
}
options = new BitmapFactory.Options();
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeFile(path, options);
}
public static Bitmap getScaledBitmap(String path, Activity activity)
{
android.graphics.Point size = new android.graphics.Point();
activity.getWindowManager().getDefaultDisplay().getSize(size);
return getScaledBitmap(path, size.x, size.y);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论