【问题标题】:How to set an image to imageView using filepath in android如何在android中使用文件路径将图像设置为imageView
【发布时间】:2013-04-04 14:51:37
【问题描述】:

我正在使用浏览按钮获取图像的文件路径....之后我想使用文件路径将此图像设置为图像视图

【问题讨论】:

    标签: android


    【解决方案1】:

    如果File 是指File 对象,我会尝试:

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

    【讨论】:

    • Uri imageUri = Uri.parse(ImagePath); imageView.setImageURI(imageUri); ...这适用于我的所有设备,除了小米 Mi4,不知道为什么。
    • 这不适用于我设备中的大图像(例如:1.5MB 或 2MB)。任何压缩方式(我只想显示缩略图)。
    • 它没有设置正确的图像尺寸,Lakshay Sharma 的回答对于图像尺寸来说是完美的
    【解决方案2】:

    你可以试试这个代码:

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

    BitmapFactory 会将给定的图像文件解码为 Bitmap 对象,然后您将其设置到 imageView 对象中。

    【讨论】:

    • 这是最简单的方法。显然,如果您只返回文件而不是路径。使用file.getPath() 而不是yourFilePath
    【解决方案3】:

    要从文件中设置图像,您需要这样做:

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

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

    您可以使用数字(在本例中为 500 和 250)来更改ImageView 的位图质量。

    【讨论】:

      【解决方案4】:

      从文件加载图像:

      Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture);
      

      假设您的pathToPicture 是正确的,那么您可以将此位图图像添加到ImageView 之类的

      ImageView imageView = (ImageView) getActivity().findViewById(R.id.imageView);
      imageView.setImageBitmap(BitmapFactory.decodeFile(pathToPicture));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多