1

What I am trying to do is put multiple images without the out of memory problem so I'm using the method PutImage() and trying to put the image in the ImageView but from the Activity not the XML file

Here is my XML code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"       android:alwaysDrawnWithCache="false">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    />

</RelativeLayout>

And here is the activity

public class RecipeBanana extends Activity {
ImageView v;
Button button;
Context localcontext = null;     
public static Drawable getAssetImage(Context context, String filename) throws IOException {
AssetManager assets = context.getResources().getAssets();
InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".jpg")));
Bitmap bitmap = BitmapFactory.decodeStream(buffer);
return new BitmapDrawable(bitmap);
}


public void PutImage(ImageView v, String x){

    try {
        v.setImageDrawable(Drawable.createFromStream(localcontext.getAssets().open("flags/" + x + ".jpg"), null));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_banana);

        ImageView v=(ImageView) findViewById(R.id.imageView1);
        PutImage(v,"bananarecipe");
        addListenerOnButton();

    }
4

1 に答える 1

0

getAssetImage() を呼び出すことはないので、そのメソッドは必要ありません。削除してください。コンテキストへの参照を保持するために変数を作成する必要はありません。アクティビティコンテキストであるためgetAssets()、前に何も置く必要なく直接呼び出すことができます。

このようにしてみてください:

public class RecipeBanana extends Activity {
    ImageView v;
    Button button; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_banana);
        ImageView v=(ImageView) findViewById(R.id.imageView1);
        PutImage(v,"bananarecipe");
        addListenerOnButton();
    }
    public void PutImage(ImageView v, String filename){
        try {
            v.setImageDrawable(Drawable.createFromStream(getAssets().open("flags/" + filename + ".jpg"), null));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

bananarecipe.jpg内部で呼び出されたファイルがある場合、このコードを使用するとassets/flags/、ImageView に読み込まれます。

于 2013-04-08T23:09:28.227 に答える