2011-08-25 78 views
3

是否可以打開一個圖片,該圖片存儲在resview/drawable文件夾中與Android構建在圖像查看器?我不明白它的工作。我試過follwoing方式打開圖片存儲在res/drawable與buildin Android圖像查看器

Button ButtonPhase2 = (Button) findViewById(R.id.button1); 
    ButtonPhase2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 

      Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW); 
      myIntent.setData(Uri.parse("android.resource://de.test.app/" + R.drawable.testbild)); 
      myIntent.setType("image/png"); 
      startActivityForResult(myIntent, 1); 

     } 

    }); 

但我得到的錯誤:

Unable to start Activity ComponentInfo{com.android.gallery/com.android.camera.ViewImage}: java.lang.nullPointerException 

我認爲這是錯誤的道路?

回答

-3

雅,你可以用這種方式做我: 首先你需要在XML中聲明佈局

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" 
    android:gravity="center" android:orientation="horizontal"> 
    <ImageButton android:id="@+id/anim_btn_play" 
     android:layout_width="155dp" android:layout_height="80dp" 
     android:layout_marginLeft="260dp" android:layout_marginTop="150dp" 
     > 
    </ImageButton> 

</LinearLayout> 

的Java文件:

ImageButton tns_imgv_back; 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.touch_and_show); 
     { 

     tns_imgv_back= (ImageButton)findViewById(R.id.anim_btn_play) 
     tns_imgv_back.setImageResource(R.drawable.icon); 
      anim_btn_play11.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 


             // you can do any thing over here when you click it will fire 

              } 
      }); 

    }} 
+0

你可能請更準確地定義這一點,因爲當我這樣做時,當我點擊圖標時,不會在構建Android圖像查看器中打開某些內容。 – Samson

+0

我已更新我的代碼 –

相關問題