Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

java - Android detect the image is png/jpg/etc extension from the web

I have to read the image from my website. However, the images uploaded by users in the server have variety extensions. jpg png etc. How I can modify the image_url code to detect the extension of the image automatically without having to declare it as static

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);

This is because the name of the image file is not fixed. So how can I display it properly the image with its correct extension.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You just check your Image_url ends with .png or not by using .endsWith(....) method of String like

if(image_url.endsWith(".png")){
//your image ends with .png

}else if(image_url.endsWith(".jpg")){
//your image  ends with .jpg
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...