【问题标题】:android get Bitmap or sound from assetsandroid从资产中获取位图或声音
【发布时间】:2012-01-20 00:58:04
【问题描述】:

我需要从资产中获取位图和声音。我尝试这样做:

BitmapFactory.decodeFile("file:///android_asset/Files/Numbers/l1.png");

像这样:

getBitmapFromAsset("Files/Numbers/l1.png");
    private Bitmap getBitmapFromAsset(String strName) {
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            istr = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
    }

但我得到的只是可用空间,而不是图像。

如何做到这一点?

【问题讨论】:

    标签: android audio assets android-bitmap


    【解决方案1】:
    public static Bitmap getBitmapFromAsset(Context context, String filePath) {
        AssetManager assetManager = context.getAssets();
    
        InputStream istr;
        Bitmap bitmap = null;
        try {
            istr = assetManager.open(filePath);
            bitmap = BitmapFactory.decodeStream(istr);
        } catch (IOException e) {
            // handle exception
        }
    
        return bitmap;
    }
    

    路径就是您的文件名 fx bitmap.png。如果你使用子文件夹 bitmap/ 那么它的 bitmap/bitmap.png

    【讨论】:

    • 这是正确的方法。但我只看到可用空间而不是图片......我做错了什么?
    • 检查您的图片...尝试使用调试并逐步完成。提供更多详细信息,例如您的图片放置位置和名称。
    • 如果使用子文件夹,请记住区分大小写
    • 不错的代码,但我更喜欢在 Log.e("MYAPP", "exception", e); 之前记录异常并返回 null 和 return null;
    • @georgiecasey hehe 是的,也许它应该是://填写你喜欢的任何方式。
    【解决方案2】:

    使用此代码其工作

    try {
        InputStream bitmap=getAssets().open("icon.png");
        Bitmap bit=BitmapFactory.decodeStream(bitmap);
        img.setImageBitmap(bit);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    

    更新

    在解码 Bitmap 时,如果 Image size 非常大,我们经常会遇到内存溢出异常。所以阅读文章How to display Image efficiently会对你有所帮助。

    【讨论】:

      【解决方案3】:

      接受的答案永远不会关闭InputStream。这是在资产文件夹中获取Bitmap 的实用方法:

      /**
       * Retrieve a bitmap from assets.
       * 
       * @param mgr
       *            The {@link AssetManager} obtained via {@link Context#getAssets()}
       * @param path
       *            The path to the asset.
       * @return The {@link Bitmap} or {@code null} if we failed to decode the file.
       */
      public static Bitmap getBitmapFromAsset(AssetManager mgr, String path) {
          InputStream is = null;
          Bitmap bitmap = null;
          try {
              is = mgr.open(path);
              bitmap = BitmapFactory.decodeStream(is);
          } catch (final IOException e) {
              bitmap = null;
          } finally {
              if (is != null) {
                  try {
                      is.close();
                  } catch (IOException ignored) {
                  }
              }
          }
          return bitmap;
      }
      

      【讨论】:

      • 这应该是公认的答案!您不想关闭流,因为您以后可能会遇到的异常类型是本机异常,这意味着您将无法捕获它:(
      【解决方案4】:

      Kotlin 方式,自动关闭输入流。您必须自己处理异常。

      private fun getBitmapFromAsset(context: Context, path: String): Bitmap =
              context.assets.open(path).use { BitmapFactory.decodeStream(it) }
      

      【讨论】:

        【解决方案5】:

        短 Kotlin 版本:

        assets
           .open(name)
           .use(BitmapFactory::decodeStream)
        

        【讨论】:

          【解决方案6】:

          获取存储在 Assets 文件夹中的图像位图的方法。

                 public static Bitmap getBitmapFromAssets(Context context, String fileName, int width, int height) {
                  AssetManager assetManager = context.getAssets();
          
                  InputStream istr;
                  Bitmap bitmap = null;
                  try {
                      final BitmapFactory.Options options = new BitmapFactory.Options();
                      options.inJustDecodeBounds = true;
          
                      istr = assetManager.open(fileName);
          
                      // Calculate inSampleSize
                      options.inSampleSize = calculateInSampleSize(options, width, height);
          
                      // Decode bitmap with inSampleSize set
                      options.inJustDecodeBounds = false;
                      return BitmapFactory.decodeStream(istr, null, options);
                  } catch (IOException e) {
                      Log.e("hello", "Exception: " + e.getMessage());
                  }
          
                  return null;
              }
          

          调整位图大小的方法。

           private 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) {
          
                      final int halfHeight = height / 2;
                      final int halfWidth = width / 2;
          
                      // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                      // height and width larger than the requested height and width.
                      while ((halfHeight / inSampleSize) >= reqHeight
                              && (halfWidth / inSampleSize) >= reqWidth) {
                          inSampleSize *= 2;
                      }
                  }
          
                  return inSampleSize;
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-02-19
            • 2012-06-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-11-30
            相关资源
            最近更新 更多