简体   繁体   中英

Opening a gallery from ImageButton in Android app

I am trying to make a ImageButton that opens a Gallery with images. I am trying to make it simple for now - just when I click the image to open the gallery with a couple of pictures. But I am not sure how to do it. I have searched on the Internet and saw that others are having the XML code that invokes one Java method - OnClick. I have the XML here:

<ImageButton
   android:id="@+id/imageView5"
   android:layout_width="@dimen/overview_schedule_image_width"
   android:layout_height="match_parent"
   android:layout_marginTop="10dp"
   android:src="@drawable/red"
   android:onClick="buttonClick" />

There is more code in the XML, but I think that's the one that is necessary. So far it works good. But now I am not sure where to place that "buttonClick" method and what it should contain. I have made this class:

package org.imageGallery;

import org.imageGallery.actionbarcompat.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;

public class ButtonClickActivity extends ActionBarActivity implements OnItemSelectedListener, OnClickListener{

@Override
public void onClick(View v) {

}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
}

I guess the "buttonClick" method should be placed here and it should take the images. But how do I return them to the XML and how can I display them in the gallery ? I am still very new to Android, but hopefully I will manage to do it.

EDIT:

Here is a great tutorial that helped me a lot.

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** you can use your uri */

EDIT: For images/*

public class sdActivity extends Activity implements MediaScannerConnectionClient{
    public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE = "*/*";

private MediaScannerConnection conn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    File folder = new File("/sdcard/youfoldername/");
    allFiles = folder.list();
 //   uriAllFiles= new Uri[allFiles.length];
    for(int i=0;i<allFiles.length;i++)
    {
        Log.d("all file path"+i, allFiles[i]+allFiles.length);
    }
  //  Uri uri= Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
    SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0];
    Log.d("SCAN PATH", "Scan Path " + SCAN_PATH);
    Button scanBtn = (Button)findViewById(R.id.scanBtn);
    scanBtn.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        startScan();
    }});
    }
    private void startScan()
    {
    Log.d("Connected","success"+conn);
    if(conn!=null)
    {
    conn.disconnect();
    }
    conn = new MediaScannerConnection(this,this);
    conn.connect();
    }
@Override
public void onMediaScannerConnected() {
    Log.d("onMediaScannerConnected","success"+conn);
    conn.scanFile(SCAN_PATH, FILE_TYPE);    
}
@Override
public void onScanCompleted(String path, Uri uri) {
    try {
        Log.d("onScanCompleted",uri + "success"+conn);
        if (uri != null) 
        {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);
        startActivity(intent);
        }
        } finally 
        {
        conn.disconnect();
        conn = null;
        }
       }
}

First you have to make a class that extends activity like this:

 public class Main extends Activity{}

1. then override a method called OnCreate() which launches when the activity launches.

  1. override in onCreate block method called setContentView(R.layout.yourLayout) so the activity displays the xml file.

  2. then get a reference to the button so you can listen to it this way: Button myButton = (Button) findViewById(R.id.button1);

if you implemented OnClickListener in your class then in the implemented method:

@Override
public void onClick(View v) {
if(v.getItemId() == R.id.button1){
//do what you want here:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))) //As deeper answered to open the gallery
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM