मैं छवियों के URL से पुनर्प्राप्त एसडी कार्ड में एक छवियों को कैसे सहेज सकता हूं?मैं अपनी यूआरएल से एसडी कार्ड में एक छवि कैसे स्थानांतरित कर सकता हूं?
उत्तर
सबसे पहले आपको यह सुनिश्चित करना होगा कि आपके एप्लिकेशन को एसडीकार्ड को लिखने की अनुमति है। ऐसा करने के लिए आपको उपयोग की अनुमति अपनी एप्लिकेशन मैनिफेस्ट फ़ाइल में बाहरी संग्रहण लिखने की आवश्यकता है। 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" />
@ परेशेश: धन्यवाद, मैंने 'getExternalStorageDirectory() 'का उपयोग करने के लिए कोड अपडेट किया है। क्या आपको पता है कि यह पिछला स्लैश देता है? जैसे '/ sdcard' या'/sdcard/' – Akusete
आपका प्रश्न म्यूट है क्योंकि' Environment.getExternalStorageDirectory() '' स्ट्रिंग' वापस नहीं करता है और इसलिए आपका कोड संकलित नहीं होता है। मैंने आपके लिए अपना कोड सही किया। –
एक उचित कारण क्या है ?? –
में रखें अनुमति एक उत्कृष्ट उदाहरण 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;
}
यह वर्णन नहीं करता है कि छवि को एसडीकार्ड में कैसे सहेजना है, केवल छवि को स्मृति में कैसे डाउनलोड करें। –
इस उत्तर को 9 अपवॉट कैसे प्राप्त हुए?! ... –
- 1. मैं असेंबली में एक छवि कैसे स्थानांतरित कर सकता हूं?
- 2. एसडी कार्ड
- 3. मैं एसडी कार्ड
- 4. एसडी कार्ड
- 5. एसडी कार्ड
- 6. एसडी कार्ड
- 7. एसडी कार्ड
- 8. मैं एसडी कार्ड में एक वीडियो फ़ाइल कैसे डाउनलोड कर सकता हूं?
- 9. एसडी कार्ड
- 10. एंड्रॉइड में एसडी कार्ड से मैं एक टेक्स्ट फ़ाइल कैसे पढ़ सकता हूं?
- 11. एसडी कार्ड
- 12. एसडी कार्ड
- 13. एसडी कार्ड से फ़ाइल को कैसे हटाएं?
- 14. एसडी कार्ड
- 15. एसडी कार्ड
- 16. एसडी कार्ड
- 17. मैं एसडी कार्ड प्रोग्रामेटिक रूप से एक नई निर्देशिका कैसे बना सकता हूं?
- 18. एसडी कार्ड
- 19. मैं जावा में यूआरएल से छवि कैसे पढ़ सकता हूं?
- 20. एंड्रॉइड एसडी कार्ड व्यवहार
- 21. आवेदन एसडी कार्ड
- 22. मेरे एसडी कार्ड से प्रदर्शित आइकन
- 23. मैं एक यूआरएल कैसे एन्कोड कर सकता हूं?
- 24. एंड्रॉइड एसडी कार्ड मुद्दे
- 25. एसडी कार्ड से फ़ाइल को
- 26. कोई एसडी कार्ड
- 27. वेबदृश्य एसडी कार्ड
- 28. मैं अपने डेटाबेस को एसडी कार्ड पर कैसे रख सकता हूं और ORMLite का उपयोग कैसे कर सकता हूं?
- 29. एसडी कार्ड लिखने का प्रदर्शन
- 30. एसडी कार्ड में एंड्रॉइड सेविंग बिटमैप
@Akusete ... आपको output.write (buffer, 0, buffer.length) में 'buffer.length' को प्रतिस्थापित करना चाहिए; बाइट्स पढ़ने के लिए। अन्यथा कचरा डेटा फ़ाइल के अंत में जोड़ा जाएगा। – shaffooo