如何设置来自Stream的imageView

时间:2014-05-02 12:55:28

标签: java android

我正在尝试将图像文件作为来自资产文件夹的流。然后将此流绑定到ImageView控件。最后在屏幕上显示它们。但是当我运行此代码时什么也没发生什么都不会显示。我不明白任何例外但我认为我错过了一些东西。

这就是我所拥有的

public class MainActivity extends Activity {

    private ImageView imgV=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AssetManager am=getAssets();
        try {
            String []names=am.list("image");
            InputStream is=am.open("image/"+names[0]);
            Bitmap b=BitmapFactory.decodeStream(is);
            imgV.setImageBitmap(b);
            this.addContentView(imgV, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }


@Edit
05-02 16:01:44.768: E/AndroidRuntime(2862): FATAL EXCEPTION: main
05-02 16:01:44.768: E/AndroidRuntime(2862): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vcxz/com.example.vcxz.MainActivity}: java.lang.NullPointerException
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.os.Looper.loop(Looper.java:137)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at java.lang.reflect.Method.invoke(Method.java:525)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at dalvik.system.NativeStart.main(Native Method)
05-02 16:01:44.768: E/AndroidRuntime(2862): Caused by: java.lang.NullPointerException
05-02 16:01:44.768: E/AndroidRuntime(2862):     at com.example.vcxz.MainActivity.onCreate(MainActivity.java:39)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.app.Activity.performCreate(Activity.java:5133)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 16:01:44.768: E/AndroidRuntime(2862):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-02 16:01:44.768: E/AndroidRuntime(2862):     ... 11 more

1 个答案:

答案 0 :(得分:0)

实际上你应该得到一个FileNotFoundException可能是你应该重新连接你正在测试的设备。

您的文件位于"图像"资产中的文件夹。但是你试图直接从资产开放。您还应首先创建ImageView。写下这段代码就可以了。

    AssetManager am = getResources().getAssets();
    try {
        imgV = new ImageView(getApplicationContext());
        String[] names = am.list("image");          
        InputStream is = am.open("image/"+names[0]);
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        imgV.setImageBitmap(bitmap);
        is.close();
        this.addContentView(imgV, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    } catch (IOException e) {
        e.printStackTrace();
    }