2014-01-31 42 views
0

在android中是新的,任何人都可以告诉程序添加背景图像到一个动态按钮。我的代码用于回顾json并创建button.i添加image_url是我的dadabase列和我添加图像名称为imge.png。现在我得到的图像字符串,我不知道如何检索图像。如何将数据库图像添加到动态按钮的背景

    JSONArray jsonMainNode1 = jsonResponse.getJSONArray("menu"); 

       int lengthJsonArr = jsonMainNode1.length(); 
        for(int i=0; i <lengthJsonArr; i++) 
        {          
         JSONObject jsonChildNode = jsonMainNode1.getJSONObject(i); 



          String Pid  = jsonChildNode.optString("pid".toString()); 
          String Name  = jsonChildNode.optString("name").toString(); 
          String Refid=jsonChildNode.optString("refid".toString()); 
          String image  = jsonChildNode.optString("image_url").toString(); 


          OutputData = Name+image; 
         str_id = Pid; 

        LinearLayout buttonContainer=(LinearLayout)findViewById(R.id.btn_container); 
        Button button = new Button(buttonContainer.getContext()); 

        button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
        ViewGroup.LayoutParams params = button.getLayoutParams(); 
        //Button new width 
        params.width = 64; 
        params.height = 64; 
        button.setLayoutParams(params); 
        button.setText(OutputData); 
        button.setTag(Refid); 
        button.setTextColor(Color.parseColor("#FEFCFF")); 
        button.setBackgroundResource(R.drawable.button_color); 

        button.setBackgroundDrawable(ImgDrawableFromFile(
          getResources(), "/data/data/com.example.proj2/images")); 



        buttonContainer.addView(button);   

        button.setOnClickListener(new OnClickListener() { 
          public void onClick(View v) { 
           LinearLayout buttonContainer = (LinearLayout)findViewById(R.id.btn_container); 
           buttonContainer.removeAllViews(); 
           Toast.makeText(getApplicationContext(), 
             "button" +v.getTag()+ "is clicked", Toast.LENGTH_SHORT).show(); 


           new LongOperation().execute("http://10.0.2.2:80/android_connect/home2.php?pid="+v.getTag());  


          } 
          public Drawable ImgDrawableFromFile(Resources res, String file_path) { 
           File imgFile = new File(file_path); 
           if (imgFile.exists()) { 

            myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
            if (myBitmap != null) 
             return new BitmapDrawable(res, myBitmap); 
            else 
             return null; 
           } 
           return null; 

          } 

          } 



         ); 

        } 
       } 
       } 

       //if closing bracket      
       catch (JSONException e) { 

         e.printStackTrace(); 
        } 

       } 

     } 

     private Drawable ImgDrawableFromFile(Resources resources, String string) { 

      return null; 
     } 

     } 
public void newclick(View v){ 

    Button backbutt=(Button)findViewById(R.id.button1); 
    backbutt.setTag(str_id); 
    LinearLayout buttonContainer = (LinearLayout)findViewById(R.id.btn_container); 
     buttonContainer.removeAllViews(); 

    new LongOperation().execute("http://10.0.2.2:80/android_connect/home2.php?rid="+ v.getTag());  

    { 
     Toast.makeText(getApplicationContext(),"button"+ v.getTag() + "is clicked",Toast.LENGTH_SHORT).show(); 
    } 
    //LinearLayout back_button = (LinearLayout)findViewById(R.id.butt_layout); 





} 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 
+0

上传您的新代码。你实施了什么? –

回答

1

试试这个方法:从Storage

button.setBackgroundDrawable(ImgDrawableFromFile(
          getResources(), your image path) 

和负荷的形象:像下面创建Drawable

public Drawable ImgDrawableFromFile(Resources res, String file_path) { 
    File imgFile = new File(file_path); 
    if (imgFile.exists()) { 

     myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
     if (myBitmap != null) 
      return new BitmapDrawable(res, myBitmap); 
     else 
      return null; 
    } 
    return null; 

} 

,如果你想从Assets载入图像,然后下面使用:

private Drawable getBitmapFromAsset(Resources res,String strName) 
{ 
AssetManager assetManager = getAssets(); 
InputStream istr = null; 
try { 
istr = assetManager.open(strName); 
} catch (IOException e) { 
e.printStackTrace(); 
} 

Bitmap bitmap = BitmapFactory.decodeStream(istr); 
if (bitmap != null) 
return new BitmapDrawable(res, bitmap); 
else 
return null; 
} 
+0

图像路径,我存储图像? – ammu

+0

你存储的图像在哪里? –

+0

我存储在资产 – ammu