android - how to get url from the file into bitmap object -
I am new to Android. I am downloading images from the Internet in the list V. I am getting the URL in the file object But when I send it to bitmap object, the bitmap object returns, so I mean the image is not loaded in the bitmap object. I answer. The code is here:
Private bitmap getBitmap (string URL) {string filename = string.value (); File f = new file (cache directory, filename); // Here in the F I get the image URL / here the URL in the bitmap is not loaded & amp; Get tap bitmap bitmap = bitmapfile Decodefile (f.getPath ()); If (bitmap! = Null) returns bitmap; // no, try to download {bitmap = bitmapFactory.decodeStream (new URL (url) .openConnection (). GetInputStream ()) to download it; // Save the bitmap in the cache for later file (bitmap, f); Return bitmap; } Hold (Exception pre) {ex.printStackTrace (); Return tap; }} Private Zero write file (bitmap BMP, filef) {FileOutputStream out = null; {Out} Try new FileOutputStream (f); Bmp.compress (bitmap.compress format.png, 80, out); } Hold (exception e) {e.printStackTrace (); } Finally {try {if (= out! = Null) out.close (); } I do not think you can download it (Exception ex) {}}}
Code This is a function that will take a URL that you create and it will return a drawback! It saves in a file and receives it if it exists
If not, it will be downloaded and inserted You can save the file in your folder instead It can be easily edited. / ** * Undoable draw objects in a picture url * * @ @ Drawable object / Receive personalized static drivet fruff URL (final string url) { String filename = url; File name = filename.replace ("/", "+"); File name = filename.replace (":", "+"); File name = filename.replace ("~", "s"); Last file file = new file (environment .getExternalStorage directory (file) + file separator + filename); Boolean exists = file.exists (); If (! Exists) {{url myFileUrl = try new URL (url); HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection (); Conn.setDoInput (true); Conn.connect (); InputStream = conn.getInputStream (); Last bitmap result = bitmapfinder Is the aodestream; is close(); New Thread () {Public Zero Run} {ByteArrayOutputStream Bytes = New ByteArrayOutputStream (); Result.compress (bitmap compoforat.jpeg, 40, bytes); Try {if (file.createNewFile ()) {//} else {//} FileOutputStream fo; Fo = new FileOutputStream (file); Fo.write (bytes.toByteArray ()); Fo.flush (); Fo.close (); } Hold (IOException e) {e.printStackTrace (); } } }.Start(); Bitmapdrawal returnTest = new bitmapdocumentation (result); Return return result; } Hold (FileNotFoundException e) {e.printStackTrace (); } Catch (malmarmdureleuxation E) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); } Return tap; } And {new bitmaprvote (bitmapfichter.dicodefile (file.toString ()); }}
Comments
Post a Comment