在Android应用中从ImageButton打开一个库

时间:2012-10-24 11:22:52

标签: java android gallery imagebutton

我正在尝试制作一个用图像打开图库的ImageButton。我想让它变得简单 - 就在我单击图像打开带有几张图片的图库时。但我不知道该怎么做。我在互联网上搜索过,看到其他人正在使用调用一种Java方法的XML代码 - OnClick。我在这里有XML:

<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" />

XML中有更多代码,但我认为这是必要的代码。到目前为止它运作良好。但是现在我不确定在哪里放置“buttonClick”方法以及它应包含的内容。我做了这个课:

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) {

}
}

我想“buttonClick”方法应放在这里,它应该拍摄图片。但是如何将它们返回到XML以及如何在库中显示它们?我对Android仍然很新,但希望我能设法做到这一点。

编辑:

Here is a great tutorial that helped me a lot.

2 个答案:

答案 0 :(得分:2)

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

编辑: 对于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;
        }
       }
}

答案 1 :(得分:1)

首先,您必须创建一个扩展此类活动的类:

 public class Main extends Activity{}

1。然后覆盖一个名为OnCreate()的方法,该方法在活动启动时启动。

  1. 在名为setContentView(R.layout.yourLayout)的onCreate块方法中覆盖,以便活动显示xml文件。

  2. 然后获取对该按钮的引用,以便您可以通过这种方式收听:Button myButton = (Button) findViewById(R.id.button1);

  3. 如果您在类中实现了OnClickListener,那么在实现的方法中:

    @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
    }