【问题标题】:how to open specific folders images using android default gallery如何使用android默认图库打开特定文件夹图像
【发布时间】:2011-12-08 09:35:47
【问题描述】:

我想使用默认的 android 图库应用程序在我的程序中打开特定文件夹的图像。我在帖子中使用了 piyush mishra 给出的这段代码,但我在代码下面写了问题

public class GalleryActivity extends Activity implements MediaScannerConnectionClient{
    /** Called when the activity is first created. */
     public String[] allFiles;
     private String SCAN_PATH ;
     private static final String FILE_TYPE = "images/*";

     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/images/");
         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()+"/images/"+allFiles[0];
         Log.e("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.toString() + "success"+conn);
             if (uri != null)                
             {

             Intent intent = new Intent(Intent.ACTION_DEFAULT);
             intent.setData(uri);
             startActivity(intent);
             }
             } finally 

             {
             conn.disconnect();
             conn = null;
             }


}
 }

但此代码也显示设备上存在的其他图像

【问题讨论】:

  • 在使用此之前,您需要向系统发送广播以更新图库,否则您将无法成功,我尝试了此代码及其工作得很好。

标签: android android-gallery


【解决方案1】:

我有同样的问题,我想将图库启动到包含视频和图片的特定文件夹中。

有了这个我似乎总是得到:ActivityNotFoundException

基于其他posts,我可以启动图库,但不能进入特定文件夹,也不能同时启动视频和图像。把我的头发拉出来。一直在尝试使用缩略图实现网格视图,这可能是一种选择。但是,既然画廊应用会做这些事情,为什么还要麻烦创建、线程化和缓存缩略图呢。

Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);

intent.setData(uri);
intent.setType("image/*");

startActivityForResult(intent, 0);

【讨论】:

  • 始终使用 setDataAndType 而不是数据和类型分开,因为每个都会使另一个无效。
【解决方案2】:

我认为这会有所帮助Gallery with Folder Filter

即使我正在尝试这个..我无法成功找到一个方法..搜索了很多但没有用..

【讨论】:

  • 我也尝试了与您看到的相同的代码,但它显示其他图像也可能是我做错了如果您找到正确的方法请回复。
  • 有没有人可以帮我解决这个问题。
  • 我不知道任何特定的人已经这样做了,但我已将您的问题发布在 2 个聊天室casualchat 和 android..希望有人会帮助您
  • 你好弗兰肯斯坦,祝你好运
  • no yar..我只是不需要那个功能..所以离开了..但尝试了很多,所以我首先回答...没有可能的方法..但是您可以继续尝试..因为我的话不是最终的..您是否有必要使用默认意图..因为您可以在您的视图中显示特定的文件夹图像..您可以使用 MediaStore 获取它们并在 GridView 中显示它们.. .但不知道默认视图。
【解决方案3】:

在使用它更新画廊之前发送广播。复制粘贴即可。

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file:/+ Environment.getExternalStorageDirectory())));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-12
    • 2017-12-05
    • 2013-02-17
    • 1970-01-01
    • 2021-02-21
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多