【问题标题】:Get The file from Asset Folder android从资产文件夹android获取文件
【发布时间】:2012-11-23 06:32:16
【问题描述】:

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

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

【问题讨论】:

  • 如何产生您指定的输出?是 Uri.toString 还是别的什么?

标签: android file assets


【解决方案1】:

这是您可以根据应用程序中的要求使用的方法。您还可以在 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;
    }

调用方法如:

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

【讨论】:

  • 我删除了所有调整大小的东西并设置了 Bitmap bitmap2=Bitmap.createBitmap(bitmap) ,并使用 getBitmapFromAsset(xxxx.png) ,但它崩溃了!
  • 现在我可以获取图像,但是当涉及到其他应用程序时它崩溃了,这意味着当图像传递到 Gmail 或任何它崩溃时! GMAIL CRASHES 或者 WHATSAPP CRASHES ,好像打不开位图什么的
【解决方案2】:

你可以使用这个方法:

public static void loadAssetImage(String path, ImageView imageView,
        Context context)
{
    try
    {
        Bitmap b = BitmapFactory.decodeStream(context.getAssets().open(path));
        imageView.setImageBitmap(Bitmap.createScaledBitmap(b, b.getHeight(),
                b.getHeight() * b.getHeight() / b.getWidth(), false));
    }
    catch (Exception e)
    {
        Log.e("Exception", e.getLocalizedMessage());
    }
}

在活动中调用:

FileUtils.loadAssetImage("Folder/image.png, imageView, CurrentActivity.this);

【讨论】:

    猜你喜欢
    • 2018-01-19
    • 2014-02-14
    • 2023-03-11
    • 2014-03-10
    • 2017-02-19
    • 1970-01-01
    • 2014-05-16
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多