Question

I'm new to android and I need the image from the users choice after choosing the image from any location to be the image of the image button. Can anyone provide me with a sample code to help me out? Thanks.

/** 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();
  }         
 }
}

Another thing, I am also not sure of how to save the image. Thanks.

Was it helpful?

Solution

Set an item clicked listener on the list view. http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)

On its callback you will probably be able to get the file info needed. Then it's just a matter of setting the drawable on the button. http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top