2011-11-20 42 views
6

我认为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中),我有一个错误和我的appplication崩溃。 这里是我的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:为什么我的“你好”总是被删除?

+0

那么是什么问题?它会崩溃吗?顺便说一下,你的图像看起来非常大,Android可能会在加载时出现内存不足。 –

+0

调整图像大小 - 这是一头漂亮的小猪。一个商业产品是http://www.avs4you.com/AVS-Image-Converter.aspx – mozillanerd

+0

另外,如果你只是检查logcat中的崩溃错误,它会给你一个线索?请做到这一点,如果仍然存在疑问,请发布错误消息。 – Peterdk

回答

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

+2

这就是它!你的链接对我有很大的帮助,我的问题与第二个链接相同,我使用相同的解决方案来摆脱这个问题。所以,我们必须为了良好的工作而调整太大的形象。 – FR073N

+0

我很高兴他们工作:)看起来像一个类似的问题。 –

+0

(还记得接受答案) –

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); 
    } 

} 
0

我已经搜索了几个小时,并通过了所有的链接,最后我发现了这一点。 (getView()) .load(selectedImageUri) .into(imageView);

和滑翔做所有的后端工作。它创造了我的一天。

滑翔Github上链接: https://github.com/bumptech/glide

+0

虽然这个链接可能回答这个问题,但只有链接回答在堆栈溢出,你可以通过采取链接的重要组成部分,并将其放入你的答案,这可以改善这个答案,这将确保如果链接被改变或删除,你的答案仍然是一个答案:) – WhatsThePoint

相关问题