2012-12-04 217 views
0

我創建一個壁紙應用程序的方式,因此我把一些圖像資源文件夾。我需要在點擊按鈕後逐個顯示此圖像並將其存儲在SD卡中。
我做了什麼: 我使用的ImageView與WebView中顯示圖像。首先,當我使用WebView時,我堅持設置圖像大小,因爲它顯示爲小,我需要根據設備窗口大小顯示這些圖像。
我用下面的代碼,但沒有幫助在屏幕上顯示從資產圖像​​,並將其存儲在SD卡

myWebView.loadUrl("file:///android_asset/image.html"); 
    WebSettings settings = myWebView.getSettings(); 
    settings.setUseWideViewPort(true); 
    settings.setLoadWithOverviewMode(true); 

我還設置<src img="someimage.jpg" width=""100%">調整圖像,但它並沒有幫助我。

然後,我使用ImageView顯示圖像,並能夠使用以下代碼至少以某種適當的大小顯示圖像。

InputStream ims = getAssets().open("31072011234.jpg"); 
     // load image as Drawable 
     Drawable d = Drawable.createFromStream(ims, null); 
     // set image to ImageView 
     imageView.setImageDrawable(d); 

我的問題是
這是在屏幕上顯示的ImageView或網頁視圖圖像的好方法?如何採取在陣列中的所有照片時,我不知道這個圖片的名稱,並將其存儲在SD卡 給我一些提示或引用。
在此先感謝。

+0

你爲什麼從資產加載圖像?爲什麼沒有資源? –

+0

好的..無論是從資產還是資源,但哪個是最好的展示方式? –

+0

作爲Webview是一個沉重的分量,我會更喜歡你用簡單的ImageView。 –

回答

1

你不需要把你的圖片在assets文件夾,您可以使用res/drawable來存儲您的圖像和訪問它作爲resource

使用下面的代碼,你可以從繪製圖像的訪問,而不需要知道圖像文件的名稱。

並使用下面的代碼,您可以將圖像保存到SD卡。

File file = new File(extStorageDirectory, "filename.PNG"); 
outStream = new FileOutputStream(file); 
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
outStream.flush(); 
outStream.close(); 
+0

getField()從drawable獲取所有圖像?如果我有一些.xml文件以及圖像,那麼它將只顯示圖像或顯示.xml文件? –

0

顯示的圖像是ImageView(這就是爲什麼它被稱爲一個圖片查看),我建議你在res/drawable文件夾添加圖像,並使用展示形象的最佳方式:

​​

的資源可以使用保存到SD卡:

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.id.some_image); 
String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
File file = new File(extStorageDirectory, "someimage.PNG"); 
outStream = new FileOutputStream(file); 
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
outStream.flush(); 
outStream.close(); 
相關問題