我必须从我的网站上阅读图片。但是,服务器中用户上传的图像具有各种扩展名。 jpg png等。如何修改image_url
代码以自动检测图像的扩展名,而不必将其声明为静态
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_product, container, false);
// Loader image - will be shown before loading image
int loader = R.drawable.loader;
editTxtQuantity = (EditText)rootView.findViewById(R.id.inputQuantity);
btnAddtocart = (Button) rootView.findViewById(R.id.btnAddtocart);
btnCancel = (Button) rootView.findViewById(R.id.btnCancel);
productErrorMsg = (TextView) rootView.findViewById(R.id.product_error);
txtName = (TextView) rootView.findViewById(R.id.inputName);
txtPrice = (TextView) rootView.findViewById(R.id.inputPrice);
txtDesc = (TextView) rootView.findViewById(R.id.inputDesc);
// Imageview to show
image = (ImageView) rootView.findViewById(R.id.image);
// Getting productid from ScanFragment
pid = getArguments().getString("pid");
// Image url
String image_url = "http://smartqr.droid-addict.com/upload/products/" + pid +".png";
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getActivity().getApplicationContext());
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(image_url, loader, image);
这是因为图像文件的名称不固定。那么如何以正确的扩展名正确显示图像呢?
答案 0 :(得分:4)
您只需使用Image_url
.png
方法{/ 1}}来检查.endsWith(....)
是否与String
结尾
if(image_url.endsWith(".png")){
//your image ends with .png
}else if(image_url.endsWith(".jpg")){
//your image ends with .jpg
}
答案 1 :(得分:1)
您可以使用String.endsWith
方法执行此操作,请参阅this链接以便更好地理解。
String image_url = "http://testing-123.com/upload/products/" + pid +".png";
if(image_url.endsWith("png"))
{
// do something
}
else if(image_url.endsWith("jpg"))
{
// do something
}
else if(image_url.endsWith("bmp"))
{
// do something
}
希望它会对你有所帮助。
答案 2 :(得分:0)
这是最好的方法,如果您要从设备中选择图像。
...
/*
* Get the file's content URI from the incoming Intent, then
* get the file's MIME type
*/
Uri returnUri = returnIntent.getData();
String mimeType = getContentResolver().getType(returnUri);
https://developer.android.com/training/secure-file-sharing/retrieve-info#java ...