如何打开图库来显示特定目录中的图像

编程入门 行业动态 更新时间:2024-10-23 12:32:17
本文介绍了如何打开图库来显示特定目录中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 带文件夹过滤器的图库

我的应用程序在目录中创建图片sdcard,我想打开画廊。为此,我只需执行以下操作:

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 =images / *

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

我已经回答了链接也特定文件夹中的内置图库。

更多推荐

如何打开图库来显示特定目录中的图像

本文发布于:2023-11-02 08:21:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   图库   目录中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!