2013-04-04 24 views

Trả lời

34

Nếu với File bạn có nghĩa là một đối tượng File, tôi sẽ cố gắng:

File file = .... 
Uri uri = Uri.fromFile(file); 
imageView.setImageURI(uri); 
+0

Uri imageUri = Uri.parse (ImagePath); imageView.setImageURI (imageUri); ... Điều này làm việc cho tất cả các thiết bị cho tôi ngoại trừ Xiaomi Mi4, bất kỳ ý tưởng tại sao là điều đó. – Avijeet

+0

Điều đó không hoạt động với hình ảnh lớn trong thiết bị của tôi (ví dụ: 1.5MB hoặc 2MB). Bất kỳ cách nào để nén nó (tôi chỉ muốn hiển thị hình thu nhỏ). –

+0

Nó không đặt kích thước hình ảnh chính xác, câu trả lời của Lakshay Sharma là hoàn hảo với kích thước hình ảnh –

8

bạn có thể đưa ra một thử mã này:

imageView.setImageBitmap(BitmapFactory.decodeFile(yourFilePath)); 

BitmapFactory sẽ giải mã các tập tin hình ảnh cho thành một đối tượng Bitmap, mà sau đó bạn sẽ đặt vào đối tượng imageView.

+2

Đây là phương pháp dễ nhất. Rõ ràng, nếu bạn chỉ trả lại tệp và không phải đường dẫn. sử dụng 'file.getPath()' thay vì 'yourFilePath' – mr0mr

7

Để thiết lập một hình ảnh từ một tập tin mà bạn cần phải làm điều này:

File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); //your image file path 
mImage = (ImageView) findViewById(R.id.imageView1); 
mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250)); 

Khi decodeSampledBitmapFromFile:

public static Bitmap decodeSampledBitmapFromFile(String path, 
     int reqWidth, int reqHeight) { // BEST QUALITY MATCH 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     options.inPreferredConfig = Bitmap.Config.RGB_565; 
     int inSampleSize = 1; 

     if (height > reqHeight) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } 

     int expectedWidth = width/inSampleSize; 

     if (expectedWidth > reqWidth) { 
      //if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize.. 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 


    options.inSampleSize = inSampleSize; 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 

    return BitmapFactory.decodeFile(path, options); 
    } 

Bạn có thể chơi với những con số (500 và 250 trong trường hợp này) để thay đổi chất lượng của bitmap cho ImageView.

1

Để tải một hình ảnh từ một tập tin:

Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture); 

Giả sử rằng bạn pathToPicture là đúng, sau đó bạn có thể thêm hình ảnh bitmap này để một ImageView như

ImageView imageView = (ImageView) getActivity().findViewById(R.id.imageView); 
imageView.setImageBitmap(BitmapFactory.decodeFile(pathToPicture)); 
Các vấn đề liên quan