2014-02-09 19 views
0

我是新來的android和我需要從用戶選擇圖像後,從任何位置選擇圖像作爲圖像按鈕的圖像。任何人都可以提供示例代碼來幫助我嗎?謝謝。Android:如何讓用戶選擇的圖像成爲ImageButton的圖像?

/** Called when the activity is first created. */ 

@Override 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.file_explorer); 

    myPath = (TextView)findViewById(R.id.action_settings); 

    getDir(root); 

} 



private void getDir(String dirPath) 

{ 

myPath.setText("Location: " + dirPath); 
item = new ArrayList<String>(); 
path = new ArrayList<String>(); 



File f = new File(dirPath); 

File[] files = f.listFiles(); 



if(!dirPath.equals(root)) 

{ 
    item.add(root); 
    path.add(root); 

    item.add("../"); 
    path.add(f.getParent()); 

} 



for(int i=0; i < files.length; i++) 

{ 

    File file = files[i]; 
    path.add(file.getPath()); 
    if(file.isDirectory()) 
    item.add(file.getName() + "/"); 
    else 
    item.add(file.getName()); 

} 

ArrayAdapter<String> fileList = 
    new ArrayAdapter<String>(this, R.layout.file_row, item); 
setListAdapter(fileList); 

} 
    protected void onListItemClick(ListView l, View v, int position, long id, String[]  writeinfo, Options options) { 

File file = new File(path.get(position)); 

if (file.isDirectory()) 
    { 
if(file.canRead()) 
getDir(path.get(position)); 
else 
{ 
new AlertDialog.Builder(this) 
.setTitle("[" + file.getName() + "] folder can't be read!") 
.setPositiveButton("OK", 
    new DialogInterface.OnClickListener() { 



    @Override 
    public void onClick(DialogInterface dialog, int which) { 
    // TODO Auto-generated method stub 
    } 
    }).show(); 
} 
} 

else 

{ 

new AlertDialog.Builder(this) 
.setIcon(R.drawable.mufc) 
.setTitle("[" + file.getName() + "]") 
.setPositiveButton("OK", 
new DialogInterface.OnClickListener() { 



    @Override 

public void onClick(DialogInterface dialog, int which) { 

    // TODO Auto-generated method stub 
    } 
    }).show(); 
    }   
} 
} 

另一件事,我也不知道如何保存圖像。謝謝。

+0

您的問題中缺少大量細節。您是否使用列表視圖向用戶顯示圖像?它在文件系統上嗎?告訴我們你已經嘗試過了。 – Diederik

+0

我已經編輯它。這是我的文件瀏覽器的代碼。它只顯示文件的位置。用於預覽的標準圖像,以及所提及的文件的名稱。 – parmadiel

回答

相關問題