0

私は1つの壁紙アプリケーションを作成しているため、アセットフォルダーに画像を入れています。ボタンをクリックしてこの画像を1つずつ表示し、SDカードに保存する必要があります。
私がしたこと: ImageView と WebView を使用して画像を表示します。まず、WebView を使用すると、画像サイズが小さく表示されるため、画像サイズの設定に固執し、デバイスのウィンドウ サイズごとに画像を表示する必要があります。
次のコードを使用しましたが、画面上の画像を調整するのに役立ちませんでした

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);

私の質問は
、画面のイメージビューまたはウェブビューに画像を表示する良い方法はどれですか?. この画像の名前がわからない場合にすべての画像を配列に取り、SDカードに保存する方法ヒントまたは参考にしてください。
前もって感謝します。

4

2 に答える 2

1

assets画像をフォルダーに入れる必要はありません。使用res/drawableして画像を保存し、resource.

以下のコードを使用すると、画像ファイルの名前を知らなくても、ドローアブルから画像にアクセスできます。

Class resources = R.drawable.class;
    Field[] fields = resources.getFields();
    String[] imageName = new String[fields.length];     
    int index = 0;
    for( Field field : fields )
    {
        imageName[index] = field.getName();
        index++;
    }

    int result = getResources().getIdentifier(imageName[10], "drawable", "com.example.name");  

以下のコードを使用すると、画像を SD カードに保存できます。

File file = new File(extStorageDirectory, "filename.PNG");
 outStream = new FileOutputStream(file);
 bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
 outStream.flush();
 outStream.close();
于 2012-12-05T06:36:48.250 に答える
0

画像を表示する最良の方法は(そのため、画像ビューImageViewと呼ばれます)、フォルダーに画像を追加し、次を使用して画像を表示することをお勧めします。res/drawable

imageView.setImageResource(R.id.some_image);

リソースは、次を使用して sdcard に保存できます。

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();
于 2012-12-04T19:59:48.737 に答える