【问题标题】:Android detect the image is png/jpg/etc extension from the webAndroid 检测图像是来自网络的 png/jpg/etc 扩展名
【发布时间】:2014-06-02 05:20:42
【问题描述】:

我必须从我的网站上读取图像。但是,用户在服务器上传的图片有多种扩展名。 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);

这是因为图像文件的名称不固定。那么如何正确显示具有正确扩展名的图像。

【问题讨论】:

  • 你有图片的全名吗?文件名+扩展名?
  • 从“testing-123.com/upload/products”获取所有文件名,然后用startsWith(pid)检查它是否是你的文件然后下载。
  • @shayanpourvatan 是的,现在我是 pid+png。我必须将所有图像转换为png。举些例子。 pid = 0001 因此 0001.png。现在我希望它自动显示图像,不管它的扩展名。

标签: java android android-fragments imageview


【解决方案1】:

您只需使用String 之类的.endsWith(....) 方法检查您的Image_url 是否以.png 结尾

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

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

【讨论】:

    【解决方案2】:

    您可以使用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
    }
    

    希望对你有帮助。

    【讨论】:

    • 但我们已将其声明为 image_url 的 .png。 endsWith 方法还会检查其他扩展名吗?当然它会使用 image_url.endsWith("png")。
    • 你能解释一下为什么你应该把它声明为.png吗? @MarkJue
    • 根据您的回答,您仍然将 image_url 声明为 String image_url = "testing-123.com/upload/products" + pid +".png"; endsWith 方法仍然检查其他扩展名吗?因为我们已将其声明为 .png?
    • 你应该检查扩展而不是声明它的静态扩展。
    • @Andrain PO 还在迷茫他想要什么?
    【解决方案3】:

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

    ...

     /*
         * 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 ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-21
      • 2014-09-06
      • 2015-06-13
      • 2013-02-10
      • 2023-03-27
      • 2021-06-29
      • 2021-07-29
      • 2016-06-28
      相关资源
      最近更新 更多