我使用此代码从 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/