本文介绍了如何在Android开放的画廊一个特定的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我'uing以下code打开了Android默认的库应用程序。它会打开所有的图像文件夹下的SD卡。我怎么只能打开一个特定的文件夹?
意向意图=新的意图(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType(图像/ *);
startActivityForResult(Intent.createChooser(意向,选择任何照片),SELECT_IMAGE_FROM_GALLERY_CONSTANT);
解决方案
你好请使用以下code。你会得到特定的文件夹中的图像。
code:
进口的java.io.File;
进口android.app.Activity;
进口android.content.Intent;
进口android.media.MediaScannerConnection;
进口android.media.MediaScannerConnection.MediaScannerConnectionClient;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.Environment;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
公共类SDCARD123Activity扩展活动实现MediaScannerConnectionClient {
公众的String [] allFiles;
私人字符串SCAN_PATH;
私有静态最后弦乐FILE_TYPE =图像/ *;
私人MediaScannerConnection康涅狄格州;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
文件夹=新的文件(/ SD卡/照片/);
allFiles = folder.list();
// uriAllFiles =新的URI [allFiles.length]
的for(int i = 0; I< allFiles.length;我++)
{
Log.d(所有文件路径+ I,allFiles [I] + allFiles.length);
}
//开放的URI = Uri.fromFile(新文件(Environment.getExternalStorageDirectory()的toString()+/ yourfoldername /+ allFiles [0]));
。SCAN_PATH = Environment.getExternalStorageDirectory()的toString()+/照片/+ allFiles [0];
的System.out.println(SCAN_PATH+ SCAN_PATH);
Log.d(扫描路径,扫描路径+ SCAN_PATH);
按钮scanBtn =(按钮)findViewById(R.id.scanBtn);
scanBtn.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
startScan();
}});
}
私人无效startScan()
{
Log.d(保持通话,成功+康恩);
如果(conn将!= NULL)
{
conn.disconnect();
}
康恩=新MediaScannerConnection(这一点,这一点);
conn.connect();
}
@覆盖
公共无效onMediaScannerConnected(){
Log.d(onMediaScannerConnected,成功+康恩);
conn.scanFile(SCAN_PATH,FILE_TYPE);
}
@覆盖
公共无效onScanCompleted(字符串路径,开放的URI){
尝试 {
Log.d(onScanCompleted,URI +成功+康恩);
的System.out.println(URI+ URI);
如果(URI!= NULL)
{
意向意图=新的意图(Intent.ACTION_VIEW);
intent.setData(URI);
startActivity(意向);
}
} 最后
{
conn.disconnect();
康恩= NULL;
}
}
}
希望这会帮助你。
I'am uing the below code to open the android default gallery app. It opens all the image folders under sdcard. How can i open only one particular folder?
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Pick any photo"), SELECT_IMAGE_FROM_GALLERY_CONSTANT);
解决方案
Hi Please use this following code . you will get particular folder image .
code :
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SDCARD123Activity extends Activity implements MediaScannerConnectionClient{
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE="image/*";
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/Photo/");
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()+"/Photo/"+allFiles[0];
System.out.println(" SCAN_PATH " +SCAN_PATH);
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);
System.out.println("URI " + uri);
if (uri != null)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
} finally
{
conn.disconnect();
conn = null;
}
}
}
Hope this will help you .
这篇关于如何在Android开放的画廊一个特定的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!