在我的 android 应用程序中,当我尝试转换图像 url 位图时,图像的大小越来越小。
InputStream imageS = new URL(url).openConnection().getInputStream();
FileOutputStream fos = new FileOutputStream(fileName);
Bitmap.createBitmap(BitmapFactory.decodeStream(imageS));
if ((new File(extra)).exists()) {
splashImage.setImageURI(Uri.parse(extra));
}
最佳答案
我正在使用以下代码,它出现在 URL 中。可能对您有用。
private Bitmap DownloadImage(String URL)
{
// System.out.println("image inside="+URL);
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// System.out.println("image last");
return bitmap;
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK)
{
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
关于android - 将图像从 url 转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13969526/