如何打开画廊展现在一个特定的目录图像 [英] How to open gallery to show images in a specific directory

查看:131
本文介绍了如何打开画廊展现在一个特定的目录图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  画廊与文件夹筛选

我的应用程序在SD卡的目录中创建的照片,我想开画廊了。要做到这一点,我只是做如下:

My app creates pictures in a directory in sdcard and I want to open gallery for it. To do that, I simply do following:

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivity(intent);  

但它示出了在设备的每个图片。是否有可能显示在目录中的文件?

But it shows every pictures in the device. Is it possible to show only files in the directory?

推荐答案

您只需要在你的活动来实现MediaScannerConnectionClient,之后你必须给该文件夹的名称里面的文件之一的确切路径这里SCAN_PATH它将扫描所有包含该文件夹中的文件并打开它内建的画廊。所以,只要给的名字,你的文件夹,你会得到里面包括视频中的所有文件。如果你想打开的映像更改 FILE_TYPE =图像/ *

You just need to implement MediaScannerConnectionClient in your activity and after that you have to give the exact path of one of the file inside that folder name here as SCAN_PATH and it will scan all the files containing in that folder and open it inside built in gallery. So just give the name of you folder and you will get all the files inside including video. If you want to open only images change FILE_TYPE="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;
        }
       }
}

我已经回答了这个环节上也<一href="http://stackoverflow.com/questions/6074270/built-in-gallery-in-specific-folder/8255674#8255674">Built-in画廊在特定的文件夹。

这篇关于如何打开画廊展现在一个特定的目录图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆