android - 如何在android中显示 Assets 文件夹中的图像?

标签 android

我已将我的图像保存在 Assets 文件夹中并尝试显示它们。我尝试了很多方法,但仍然无法显示图像。在 logcat 中它没有显示任何错误。

请帮我解决这个问题.........

AssetManager mngr = mContext.getResources().getAssets();
        is = mngr.open("test3.png");
        bitmap = BitmapFactory.decodeStream(is);
        Uri uriSavedImage=Uri.fromFile(pictures_directory);

        ((ImageView) view.findViewById(R.id.imageView1)).setImageBitmap(bitmap);

最佳答案

您可以使用 AssetManager 通过其 open() 方法获取 InputStream,然后使用 BitmapFactory 的 decodeStream() 方法 获取位图。

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 - 如何在android中显示 Assets 文件夹中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8151102/

相关文章:

android - 签署应用程序是否可以防止篡改文件?

android - PickContact 需要 android.permission.READ_CONTACTS,或者 grantUriPermission()

android - FFMPEG 输出文件不包含任何流 [Android] video concat

Android:如何将html代码转换为图片并使用ShareIntent发送此图片?

android - 如何使用 ViewModel 存储位图列表以便在生命周期变化中生存?

android - FragmentTabPager 根本不工作

android - java.lang.IllegalArgumentException : pointerIndex out of range Exception - dispatchTouchEvent

android - e.printStackTrace() 的任何替代方案;在 Android 应用程序开发中?

java - 如何在 java android 中运行进度条时禁用正在运行的 Activity

java - Android 上的离线语音识别器无法工作