我在我的活动中展示了一个简单的ImageView
:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.package.name.EditPicture">
<ImageView
android:id="@+id/problem_picture"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
在我的活动课中,这就是我设置图像的方式:
//first calculate the width and height of the screen
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
//Then, resize the image's width and height equal to that of the screen:
Picasso.with(this).load(new File(pictureLocation)).rotate(90f).resize(height,width).into(imageView);
问题是,我在模拟器中获得了所需的结果,但在我真正的Android手机中,没有显示任何内容。整个屏幕是空白的。
由于我已经将图像的大小调整为屏幕尺寸,因此加载高分辨率图像时不应该出现任何问题。为什么我的屏幕在真实设备中是空白的?
答案 0 :(得分:0)
首先检查设备上可用的图像。
File file = new File(pictureLocation);
if (file.exists()) {
Picasso.with(this).load(new File(pictureLocation)).into(imageView);
} else {
Log.d("Result", "Image not available");
}
答案 1 :(得分:0)
我建议您在此方案中使用Content Provider。 我有同样的问题,在视图中加载的图像是黑色的。存储访问框架是这里的解决方案
https://developer.android.com/guide/topics/providers/document-provider.html
答案 2 :(得分:0)
经过一番研究,这是原因和解决方案:
在真实设备中屏幕显示为空白,因为ImageView
无法加载大图像(相机为13MP,图像为3-4 MB)。我尝试了一个较小的图像(~100 KB),效果很好。令人遗憾的是毕加索和格莱德都无法做到这一点。
因此,我首先调整了图像的大小,然后将它们压缩到100 KB范围内(如果你想要完整的高清图像,你需要一个不同的方法):
/**
* getting the screen height and width, so that we could resize the image accordingly
*/
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
/**
* Getting the old photo and then resizing it to the size of the screen.
* We are also compressing it. 70 is a number between 0 to 100.
* You see, close to 0 means very low quality but very small in size image
* Close to 100 means very high quality, but the size will be big.
*/
Bitmap photo = BitmapFactory.decodeFile(pictureLocation);
photo = Bitmap.createScaledBitmap(photo, width, height, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 70, bytes);
/**
* fetching the location where this has to be saved. folder location is the location of my Pictures folder.
*/
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String smallFileLocation = folderLocation + File.separator + "IMG_" + timeStamp + ".jpg";
/**
* New file is saved at this place now.
*/
File f = new File(smallFileLocation);
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
/**
* Later, we can simply put the picture in our ImageView using Picasso or just imageView.setImageBitmap
*/
Picasso.with(this).load(new File(smallFileLocation)).rotate(90f).resize(height,width).into(imageView);
答案 3 :(得分:0)
因为您的图片加载尺寸较大(例如:4000px x 3000px)。 使用毕加索:
int width = new DeviceSize(mContext).widthPixels();
Picasso.get().load(fileUploadPath)
.resize(width, 0)
.onlyScaleDown()
.into(imageView);
-类DeviceSize-
import android.content.Context;
import android.util.DisplayMetrics;
public class DeviceSize {
private Context mContext;
private int widthPixels;
private int heightPixels;
public DeviceSize(Context context) {
mContext = context;
}
public int widthPixels() {
DisplayMetrics lDisplayMetrics = mContext.getResources().getDisplayMetrics();
widthPixels = lDisplayMetrics.widthPixels;
return widthPixels;
}
public int heightPixels() {
DisplayMetrics lDisplayMetrics = mContext.getResources().getDisplayMetrics();
heightPixels = lDisplayMetrics.heightPixels;
return heightPixels;
}
}