My Objective is:
- Need to get image path from api call in url string like: "http://serverpath.com/projectname/images/imagename.png"
- Need to send image path to api call in file object. So I am taking ArrayList only.
I should show image path in imageview when its converted into file object.
I am trying with some solutions with different file object are:
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;
}
Here all the file objects are showing failure results in comments after the statement.