2010-07-21 14 views
23

मैं छवियों के URL से पुनर्प्राप्त एसडी कार्ड में एक छवियों को कैसे सहेज सकता हूं?मैं अपनी यूआरएल से एसडी कार्ड में एक छवि कैसे स्थानांतरित कर सकता हूं?

+0

@Akusete ... आपको output.write (buffer, 0, buffer.length) में 'buffer.length' को प्रतिस्थापित करना चाहिए; बाइट्स पढ़ने के लिए। अन्यथा कचरा डेटा फ़ाइल के अंत में जोड़ा जाएगा। – shaffooo

उत्तर

46

सबसे पहले आपको यह सुनिश्चित करना होगा कि आपके एप्लिकेशन को एसडीकार्ड को लिखने की अनुमति है। ऐसा करने के लिए आपको उपयोग की अनुमति अपनी एप्लिकेशन मैनिफेस्ट फ़ाइल में बाहरी संग्रहण लिखने की आवश्यकता है। Setting Android Permissions

फिर आप एसडीकार्ड पर एक फ़ाइल में यूआरएल डाउनलोड कर सकते हैं। एक आसान तरीका है:

URL url = new URL ("file://some/path/anImage.png"); 
InputStream input = url.openStream(); 
try { 
    //The sdcard directory e.g. '/sdcard' can be used directly, or 
    //more safely abstracted with getExternalStorageDirectory() 
    File storagePath = Environment.getExternalStorageDirectory(); 
    OutputStream output = new FileOutputStream (new File(storagePath,"myImage.png")); 
    try { 
     byte[] buffer = new byte[aReasonableSize]; 
     int bytesRead = 0; 
     while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { 
      output.write(buffer, 0, bytesRead); 
     } 
    } finally { 
     output.close(); 
    } 
} finally { 
    input.close(); 
} 

संपादित करें: प्रकट

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+1

@ परेशेश: धन्यवाद, मैंने 'getExternalStorageDirectory() 'का उपयोग करने के लिए कोड अपडेट किया है। क्या आपको पता है कि यह पिछला स्लैश देता है? जैसे '/ sdcard' या'/sdcard/' – Akusete

+1

आपका प्रश्न म्यूट है क्योंकि' Environment.getExternalStorageDirectory() '' स्ट्रिंग' वापस नहीं करता है और इसलिए आपका कोड संकलित नहीं होता है। मैंने आपके लिए अपना कोड सही किया। –

+3

एक उचित कारण क्या है ?? –

8

में रखें अनुमति एक उत्कृष्ट उदाहरण Android डेवलपर के ब्लॉग पर latest post में पाया जा सकता:

static Bitmap downloadBitmap(String url) { 
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); 
    final HttpGet getRequest = new HttpGet(url); 

    try { 
     HttpResponse response = client.execute(getRequest); 
     final int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode != HttpStatus.SC_OK) { 
      Log.w("ImageDownloader", "Error " + statusCode + 
       " while retrieving bitmap from " + url); 
      return null; 
     } 

     final HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream inputStream = null; 
      try { 
       inputStream = entity.getContent(); 
       final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
       return bitmap; 
      } finally { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
       entity.consumeContent(); 
      } 
     } 
    } catch (Exception e) { 
     // Could provide a more explicit error message for IOException or 
     // IllegalStateException 
     getRequest.abort(); 
     Log.w("ImageDownloader", "Error while retrieving bitmap from " + url, 
      e.toString()); 
    } finally { 
     if (client != null) { 
      client.close(); 
     } 
    } 
    return null; 
} 
+4

यह वर्णन नहीं करता है कि छवि को एसडीकार्ड में कैसे सहेजना है, केवल छवि को स्मृति में कैसे डाउनलोड करें। –

+1

इस उत्तर को 9 अपवॉट कैसे प्राप्त हुए?! ... –

संबंधित मुद्दे