android - 从 Asset 文件夹中获取文件 android

标签 android file assets

我使用此代码从 Assets 文件夹中获取文件:

intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///android_asset/"+knowImage))

但是当打印这一行时,总是在 file:///android_asset 和来自 knowimage 的字符串之间有空格!

打印knowimage时没有空格!但是当它们结合时,结果是带有空格的,所以不能使用它

输出是这样的:

11-23 14:16:29.128: I/Share(18204): file:///android_asset/
11-23 14:16:29.128: I/Share(18204): lovingurpregnantbodyS.png

但它必须是这样的:

file:///android_asset/lovingurpregnantbodyS.png

最佳答案

这是您可以根据您在应用程序中的要求使用的方法。您还可以在 ImageView 上显示时调整图像大小。希望这对您有所帮助。

public Bitmap getBitmapFromAsset(String strName) {
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            try {
                istr = assetManager.open(strName);
            } catch (FileNotFoundException e) {
                istr = assetManager.open("noimage.png");
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        int Height = bitmap.getHeight();
        int Width = bitmap.getWidth();
        float scale = getResources().getDisplayMetrics().density;
        int dip = (int) (40 * scale + 0.5f);
        int newHeight = width - dip;
        int newWidth = width - dip;
        float scaleWidth = ((float) newWidth) / Width;
        float scaleHeight = ((float) newHeight) / Height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, Width, Height,
                matrix, true);

        return resizedBitmap;
    }

Call method like:

Imgview.setImageBitmap(getBitmapFromAsset("YourFoldername/"+ imgname + ".jpg"));

关于android - 从 Asset 文件夹中获取文件 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13524143/

相关文章:

android.content.res.Resources$NotFoundException 与最新版本的 SDK 构建工具

android - Cocos2d-x crash due to ccLayer->schedule(schedule_selector(...), ...);

C++ 头文件交互

C++ 创建文件时出错(名称基于其他文件名)

ruby-on-rails - rails : Couldn't find a file for 'twitter/bootstrap'

如果应用程序关闭,Android IntentService 无法完成任务

java - 循环播放音频Android Studio

java - 使用 Java 复制文件时结果为空

Angular 2 : assets not loaded when deployed in subdirectory

javascript - Ruby On Rails - 包含 CSS 和 JS 的最佳方式