将大图像放入 ImageView 中不起作用
我认为我的 ImageView 有问题。 我创建了一个画廊,我可以在其中触摸图像并将其放入下面的 ImageView 中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fonddegrade">
<Gallery android:layout_height="wrap_content"
android:id="@+id/gallery"
android:layout_width="fill_parent" />
<ImageView android:layout_below="@+id/gallery"
android:layout_height="wrap_content"
android:id="@+id/laphoto"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"/>
这非常适用于小图像,但不适用于大图像(3264 * 1952)。当我触摸它时(因此,尝试将其放入 ImageView 中),出现错误并且我的应用程序崩溃。 这是我显示图像的java代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.photo);
File images; // Trouver le bon endroit de stockage
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
images = new File("/sdcard/MyPerformance/Photo");
else
images = this.getFilesDir();
images.mkdirs();
File[] imagelist = images.listFiles(new FilenameFilter(){
@Override
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
mFiles = new String[imagelist.length];
for(int i = 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for(int i = 0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}
imgView = (ImageView)findViewById(R.id.laphoto);
if(mFiles.length != 0)
imgView.setImageURI(mUrls[0]);
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
imgView.setImageURI(mUrls[position]);
}
});
}
问题要么来自setImageURI(但我不认为这是原因,因为它适用于小图像),要么是由于图片的大小。
你能给我什么解决方案来解决这个问题? 我对你表示感谢。
PS:为什么我的“你好”总是被删除?
I think there is a problem with my ImageView.
i created a gallery, where I can touch an image and put it in my ImageView below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fonddegrade">
<Gallery android:layout_height="wrap_content"
android:id="@+id/gallery"
android:layout_width="fill_parent" />
<ImageView android:layout_below="@+id/gallery"
android:layout_height="wrap_content"
android:id="@+id/laphoto"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"/>
This is perfectly working with small image, but not with big image (3264 * 1952). When I touch it (so, trying to put it in the ImageView), I have an error and my appplication crash.
Here is my java code to display the image:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.photo);
File images; // Trouver le bon endroit de stockage
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
images = new File("/sdcard/MyPerformance/Photo");
else
images = this.getFilesDir();
images.mkdirs();
File[] imagelist = images.listFiles(new FilenameFilter(){
@Override
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
mFiles = new String[imagelist.length];
for(int i = 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for(int i = 0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}
imgView = (ImageView)findViewById(R.id.laphoto);
if(mFiles.length != 0)
imgView.setImageURI(mUrls[0]);
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
imgView.setImageURI(mUrls[position]);
}
});
}
Either the problem comes from the setImageURI (but I don't think this is the cause, since it's work with small image) or because of the size of the picture.
What solution can you give me to resolve this problem?
You have my thanks.
PS: Why my "Hello" are always deleted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的图像对于 Android 来说可能太大,并且内存不足。应用程序的可用内存可能低至 16Mb。您的图像需要 3264 * 1952 * 4 = ~25.5Mb(宽度高度argb)。因此最好将图像调整为较小的尺寸。
请参阅:http://android-developers.blogspot。 co.uk/2009/01/avoiding-memory-leaks.html
然后: 将图像加载到 Bitmap 对象时出现奇怪的内存不足问题< br>
最后: VM 在获取图像时内存不足从缓存中
Your image is probably too big for Android and it goes out of memory. Apps might have as low as 16Mb of usable memory. Your image takes 3264 * 1952 * 4 = ~25.5Mb (widthheightargb). So might be best to resize the images into smaller size.
See: http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
Then: Strange out of memory issue while loading an image to a Bitmap object
Finally: VM running out of memory while getting images from the cache
这是一个位图[ util 类,可帮助您处理大型位图。
Here's a bitmap[ util class to assist you with the handling of the large bitmaps.
使用 Glide 库:
Glide 完成所有后端工作。
With Glide library:
Glide does all the backend work.