【发布时间】:2017-06-29 13:04:46
【问题描述】:
我的目标是:
- 需要从 url 字符串中的 api 调用获取图像路径,例如:“http://serverpath.com/projectname/images/imagename.png”
- 需要将图像路径发送到文件对象中的 api 调用。 所以我只使用 ArrayList。
当它转换为文件对象时,我应该在 imageview 中显示图像路径。
我正在尝试一些具有不同文件对象的解决方案:
File f=null;
URL url = new URL(info4Model.getExternal_photos().get(position).getImage_path());
try {
// f = new File(url.toURI());//java.lang.IllegalArgumentException: Expected file scheme in URI:
//f = new File(url.getPath());// not showing file object in imageview
//f = new File(url.getFile());// not showing file object in imageview
f = getFileFromBitmap(info4Model.getExternal_photos().get(position).getImage_path());
//android.os.NetworkOnMainThreadException , AsyncTask option left only?
} catch (Exception e) {
}
///////////////////////////////////
public static Bitmap getBitmapFromURL(String url) {
try {
URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmapFrmUrl = BitmapFactory.decodeStream(input);
return bitmapFrmUrl;
} catch (IOException e) {
e.printStackTrace();
return null;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
File getFileFromBitmap(String url) {
Bitmap bmp = getBitmapFromURL(url);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("MMddhhmmss");
String dateAsString = simpleDateFormat.format(new Date());
File filesDir = getActivity().getFilesDir();
File imageFile = new File(filesDir, dateAsString + ".png");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return imageFile;
}
这里所有的文件对象都在声明后的cmets中显示失败结果。
【问题讨论】:
标签: android arraylist android-bitmap android-file