fifix Posted March 8, 2011 Share Posted March 8, 2011 Bonjour, Alors voilà, je suis en train de créer une classe "GestionnaireImages" qui permet de stocker dans la carte mémoire de mon téléphone une image. Sauf que l'image vient d'internet donc je la telecharge via le code suivant Bitmap image = null; try{ URL aURL = new URL(reference); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); image = BitmapFactory.decodeStream(bis); bis.close(); is.close(); } catch(IOException e) { } Mais je ne sais pas comment la stocker dans ma carte mémoire. Si quelqu'un a une idée ou un bous de code ^^ Cordialement Link to comment Share on other sites More sharing options...
devdroid Posted March 9, 2011 Share Posted March 9, 2011 Bonjour, Alors voilà, je suis en train de créer une classe "GestionnaireImages" qui permet de stocker dans la carte mémoire de mon téléphone une image. Sauf que l'image vient d'internet donc je la telecharge via le code suivant Bitmap image = null; try{ URL aURL = new URL(reference); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); image = BitmapFactory.decodeStream(bis); bis.close(); is.close(); } catch(IOException e) { } Mais je ne sais pas comment la stocker dans ma carte mémoire. Si quelqu'un a une idée ou un bous de code ^^ Cordialement A partir de ton InputStream tu peux tester quelques chose comme ca : InputStream in = conn.getInputStream(); byte[] buf = new byte[size]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((len = in.read(buf, 0, size)) != -1) { bos.write(buf, 0, len); fileSize += len; } //TODO : Write directly to File OutputStream outputStream = new FileOutputStream (outFile); bos.writeTo(outputStream); outputStream.close(); Link to comment Share on other sites More sharing options...
fifix Posted March 9, 2011 Author Share Posted March 9, 2011 Merci J'ai reussi à m'inspirer d'autres codes ainsi que du tien et voici la solution Bitmap image = null; try{ URL aURL = new URL(reference); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); image = BitmapFactory.decodeStream(bis); bis.close(); is.close(); Toast.makeText(lecontext, image.getConfig().name(), 2000).show(); String nameFile = "myImage2"; FileOutputStream fileOutputStream = null; BitmapFactory.Options options=new BitmapFactory.Options(); fileOutputStream = new FileOutputStream("/data/data/com.portailcommunication.mangaslight/files/"+nameFile + ".jpg"); BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); image.compress(CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); } catch(IOException e) { Toast.makeText(lecontext, ""+e, 2000).show(); } Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.