2011-11-20 37 views
6

Tôi nghĩ rằng có vấn đề với ImageView của tôi. tôi đã tạo ra một bộ sưu tập, nơi tôi có thể chạm vào một hình ảnh và đặt nó trong ImageView của tôi dưới đây:Đặt một hình ảnh lớn trong ImageView không hoạt động

<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"/> 

này là hoàn toàn làm việc với hình ảnh nhỏ, nhưng không phải với hình ảnh lớn (3264 * 1952). Khi tôi chạm vào nó (vì vậy, cố gắng đặt nó trong ImageView), tôi gặp lỗi và lỗi ứng dụng của tôi. Đây là mã java của tôi để hiển thị hình ảnh:

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

Hoặc là vấn đề xuất phát từ setImageURI (nhưng tôi không nghĩ rằng đây là nguyên nhân, vì nó làm việc với hình ảnh nhỏ) hoặc vì kích thước của bức tranh.

Giải pháp nào bạn có thể cho tôi giải quyết vấn đề này? Bạn có lời cảm ơn của tôi.

PS: Tại sao tôi "Hello" luôn bị xóa?

+0

Vì vậy, vấn đề là gì? Nó có bị hỏng không? Btw, hình ảnh của bạn trông rất lớn, Android có thể hết bộ nhớ trong khi tải nó. –

+0

Pre resize hình ảnh - cái này khá là heo. Một sản phẩm thương mại là http://www.avs4you.com/AVS-Image-Converter.aspx – mozillanerd

+0

Ngoài ra, nếu bạn chỉ cần kiểm tra các lỗi tai nạn trong logcat, nó sẽ cung cấp cho bạn một đầu mối ?? Hãy làm điều đó và đăng lỗi nếu vẫn còn nghi ngờ. – Peterdk

Trả lời

8

Hình ảnh của bạn có thể quá lớn đối với Android và nó hết bộ nhớ. Ứng dụng có thể có tới 16 triệu bộ nhớ có thể sử dụng. Hình ảnh của bạn mất 3264 * 1952 * 4 = ~ 25.5Mb (chiều rộng chiều cao argb). Vì vậy, có thể là tốt nhất để thay đổi kích thước hình ảnh thành kích thước nhỏ hơn.

Xem: http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
Sau đó: Strange out of memory issue while loading an image to a Bitmap object
Cuối cùng: VM running out of memory while getting images from the cache

+2

Đây là nó! Các liên kết của bạn đã giúp tôi rất nhiều, vấn đề của tôi giống như liên kết thứ hai và tôi đã sử dụng cùng một giải pháp để loại bỏ vấn đề đó. Vì vậy, chúng tôi phải quy mô hình ảnh quá lớn để làm việc tốt. – FR073N

+0

Tôi rất vui vì họ đã làm việc :) Dường như một vấn đề tương tự. –

+0

(Cũng nhớ chấp nhận câu trả lời) –

1

Dưới đây là một bitmap [util lớp để hỗ trợ bạn với việc xử lý các bitmap lớn.

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

Tôi đã tìm kiếm hàng giờ và xem xét tất cả các liên kết và cuối cùng tôi đã tìm thấy điều này.

Glide.with (getContext()) .load (selectedImageUri) .into (imageView);

và trượt làm tất cả công việc phụ trợ. Nó làm cho ngày của tôi.

Glide Github Link: https://github.com/bumptech/glide

+0

Trong khi liên kết này có thể trả lời câu hỏi, chỉ có câu trả lời liên kết được ngăn cản trên Stack Overflow, bạn có thể cải thiện câu trả lời này bằng cách lấy các phần quan trọng của liên kết và đưa nó vào câu trả lời của bạn câu trả lời của bạn vẫn là câu trả lời nếu liên kết bị thay đổi hoặc bị xóa :) – WhatsThePoint

Các vấn đề liên quan