1

このコードを使用して、アセット フォルダーからファイルを取得します。

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
4

2 に答える 2

4

これは、アプリケーションの要件に従って使用できる方法です。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"));
于 2012-11-23T07:10:02.460 に答える
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);
于 2012-11-23T07:29:20.683 に答える