Java Android检测图像是来自web的png/jpg/etc扩展

Java Android检测图像是来自web的png/jpg/etc扩展,java,android,android-fragments,imageview,Java,Android,Android Fragments,Imageview,我必须阅读我网站上的图片。但是,用户在服务器中上载的图像具有多种扩展。jpg png等。我如何修改图像\u url代码,以自动检测图像的扩展,而不必将其声明为静态 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_prod

我必须阅读我网站上的图片。但是,用户在服务器中上载的图像具有多种扩展。jpg png等。我如何修改
图像\u 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);

这是因为图像文件的名称不是固定的。那么,我怎样才能正确地显示图像及其正确的扩展名呢

您只需检查您的
图像\u url
.png
结尾,或者不使用
.endsWith(..)
方法的
字符串

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

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

您可以使用
String.endsWith
方法执行此操作,请参阅链接以获得更好的理解

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
}

希望它能帮助您。

如果您要从设备中选择图像,这是最好的方法。


您有图像的全名吗?文件名+扩展名?从“”获取所有文件名,然后使用
startsWith(pid)
检查是否是您的文件,然后下载。@shayanpourvatan是的,现在我使用pid+png。我必须将所有图像转换为png。例如。pid=0001,因此为0001.png。现在我想让它自动显示图像,不管它的扩展名是什么。但是我们已经将它声明为.png作为图像的url。endsWith方法是否仍会检查其他扩展?当然,它将用于image_url.endsWith(“png”)。您能解释一下为什么要将其声明为.png吗@markjuea根据您的回答,您仍然将image_url声明为字符串image_url=“”+pid+“.png”;endsWith方法是否仍在检查其他扩展?既然我们已经将其声明为.png?您应该检查扩展名,而不是声明其静态扩展名。@而且Rain PO仍然不明白他想要什么?
 /*
     * 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);