Java tutorial
//package com.java2s; /* * Copyright (C) 2014-present, Wei Chou (weichou2010@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Locale; import android.annotation.SuppressLint; import android.graphics.Bitmap.CompressFormat; import android.os.Build; import android.webkit.URLUtil; public class Main { public static String guessImageFormat(String urlOrPath) { CompressFormat format = guessImageFormatC(urlOrPath); if (format == null) return null; if (format == CompressFormat.JPEG) return "jpg"; return format.name().toLowerCase(Locale.US); } @SuppressLint("NewApi") public static CompressFormat guessImageFormatC(String urlOrPath) { CompressFormat format = null; String fileName; if (URLUtil.isNetworkUrl(urlOrPath)) { fileName = URLUtil.guessFileName(urlOrPath, null, null); } else if (urlOrPath.lastIndexOf('.') <= 0) { return null; } else { fileName = urlOrPath; } fileName = fileName.toLowerCase(Locale.US); if (fileName.endsWith(".png")) { format = CompressFormat.PNG; } else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) { format = CompressFormat.JPEG; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && fileName.endsWith(".webp")) { format = CompressFormat.WEBP; } return format; } }