从网址下载图片时会发生一些有趣的事情。我目前在将图片发布到
时将SkAndroidCodec :: NewFromStream返回nullcreateBitmap用于创建文件的位图(这是主要问题)
private Bitmap createBitmap(String imageFile){
try {
File f = new File(imageFile);
InputStream l = new FileInputStream(f);
BufferedReader bf = new BufferedReader(new FileReader(f));
String str = "";
while((str = bf.readLine()) != null){
Log.d("testing",str);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BufferedInputStream bs = new BufferedInputStream(l);
BitmapFactory.decodeStream(bs,null,options);
bs.reset();
//boolean support = l.markSupported();
//l.reset();
options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(l,null,options);
}catch(Exception ex){
Log.d("Exception",ex.toString());
return null;
}
}
urlImage
函数用于在ImageView
private boolean urlImage(String url) {
String[] test = url.split("/");
String imgFile = mainDir.concat("/".concat(test[test.length - 1]));
File f = new File(imgFile);
Log.d("Image Src", imgFile);
String t = (f.exists()) ? "true" : "false";
Log.d("Exists", t);
try {
if (!f.exists()) {
f.createNewFile();
URL urlImage = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlImage.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
conn.setInstanceFollowRedirects(false);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedWriter bw = new BufferedWriter(new FileWriter(imgFile));
String str = "";
while ((str = br.readLine()) != null) {
bw.write(str);
Log.d("something else",str);
}
bw.close();
os.close();
Bitmap m = BitmapFactory.decodeStream(is);
is.close();
int responseCode = conn.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK) {
Log.d("HTTP OK","responsed properly");
imageSrc.setImageBitmap(m);
}
createBitmap(imgFile);
}
readFile(imgFile,"image");
return true;
} catch (Exception ex) {
Log.d("Exception", ex.toString());
return false;
}
}
readFile
下载后创建文件的功能
private void readFile(String file,String fileType){
Log.d("File",file);
try {
Log.d("createBitmap","bitmap being created");
if(fileType == "image") {
mainBitmap = createBitmap(file);
imageSrc.setImageBitmap(mainBitmap);
}else{
readTextFile(file);
}
}catch(Exception ex){
Log.e("Exception",ex.toString());
}
}
真的不确定导致问题的原因。跟随此处发布的可能导致问题的所有错误,解决方案和我无法解决问题。
答案 0 :(得分:0)
无法弄清问题是什么,但是这里有一些非常有趣的选项可以提供错误的信息。
不应该有双重缓冲图像,特别是如果您已经创建了图像文件。
虽然应该能够从HTTPConnect写入图像文件,但是无法通过直接写入文件来确定文件是否是图像。 (可能是逐行抓取而没有创建新行的问题(或者它可能只是它以不同的方式处理信息,无论它是否作为实际图像文件出错))
还有一些其他非常大的问题可以通过android studio系统的内置命令修复,但实际上需要使用其中一个系统进程来创建一个图像文件将违反用于整合的某些编码实践文件类型。
我猜这是android系统的创造者认为需要从1990年代早期使用的现已解散的jpeg病毒中删除添加代码的东西
这将是以标准方式处理图像所需的代码。
private File createImageFile(String url) throws IOException{
Date date = new Date();
String[] urlArray = url.split("/");
String imgFileString = mainDir.getPath().toString().concat("/".concat(urlArray[urlArray.length - 1]));
File f = new File(imgFileString);
Log.d("Image File Tag",imgFileString);
if (!f.isFile()) {
String[] test = url.split("/");
String imgFile = mainDir.getPath().toString().concat("/".concat(test[test.length - 1]));
URL urlImage = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlImage.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
conn.setInstanceFollowRedirects(false);
Log.d("testing", conn.getContent().toString());
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Log.d("HTTP OK", "responded properly");
}
Bitmap btm = BitmapFactory.decodeStream((InputStream) urlImage.getContent());
FileOutputStream fos = new FileOutputStream(f);
btm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
}
return f;
}