0
Im旨在創建一個應用程序,允許我從一組網址下載多個圖像並允許用戶在其中滑動。在Kindle Fire上下載並保存圖像
我有點不確定如何做到這一點,因爲我不知道如何將圖像保存在點燃(它沒有SD卡)。
任何關於如何從本地保存網絡圖像(儘快訪問)的幫助將會很棒!
Im旨在創建一個應用程序,允許我從一組網址下載多個圖像並允許用戶在其中滑動。在Kindle Fire上下載並保存圖像
我有點不確定如何做到這一點,因爲我不知道如何將圖像保存在點燃(它沒有SD卡)。
任何關於如何從本地保存網絡圖像(儘快訪問)的幫助將會很棒!
你可以在你的數組中循環使用這個方法。不要擔心外部目錄。沒有SD卡插槽的設備與內部存儲器分開放置,以充當「外部存儲器」。
public Bitmap downloadImage(String url)
{
final DefaultHttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
try
{
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK)
{
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream inputStream = null;
try
{
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
saveImageToExternalMemory(bitmap, url); //edit the name if need
return bitmap;
}
finally
{
if (inputStream != null)
{
inputStream.close();
}
entity.consumeContent();
}
}
}
catch(IOException e)
{
getRequest.abort();
}
catch (Exception e)
{
getRequest.abort();
}
finally
{
if (client != null)
{
client.getConnectionManager().shutdown();
}
}
return null;
}
這會將圖像保存爲url的名稱,您可以根據需要進行編輯。並將圖像保存到外部存儲器中(無論設備是否有SD卡都無關緊要)。例如,我有一個Nexus 7,它的工作原理。
public void saveImageToExternalMemory(Bitmap bitmap, String name) throws IOException
{
File dir = new File(Environment.getExternalStorageDirectory().toString()+"/yourdirectoryname");
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, name+ ".jpg"); //or the type you need
file.createNewFile();
OutputStream outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
}
這種方法需要
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
在清單
,並下載要求:
<uses-permission android:name="android.permission.INTERNET"/>
只是看我的答案:) –
是它有用嗎? –
我很抱歉拖延...它工作!非常感謝! – DeeBrad