2011-11-20 65 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

相關問題