我使用此代码从资产文件夹中获取文件:
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///android_asset/"+knowImage))
但是当打印这一行时,总是在file:/// android_asset和来自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
答案 0 :(得分: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"));
答案 1 :(得分: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);