我认为我的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:为什么我的“Hello”总是被删除?
答案 0 :(得分:8)
您的图片可能对Android而言太大而且内存不足。应用程序可能具有低至16Mb的可用内存。您的图像需要3264 * 1952 * 4 = ~25.5Mb(宽度高度 argb)。因此最好将图像调整为更小的尺寸。
请参阅:http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
然后:Strange out of memory issue while loading an image to a Bitmap object
最后:VM running out of memory while getting images from the cache
答案 1 :(得分:1)
这是一个位图[util类,可以帮助您处理大位图。
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class BitmapUtils {
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String pathToFile,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathToFile, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathToFile, options);
}
}