打开图像表单built_in gallery

时间:2011-08-20 12:16:50

标签: android gallery

我已阅读此链接:以编程方式Get/pick an image from Android's built-in Gallery app programmatically在Android的内置图库应用中打开图片,代码看起来很顺利。

结果如下图所示:http://i.stack.imgur.com/vz3S8.png,但这不是我想要的结果。

我想打开类似于http://i.stack.imgur.com/ZoUvU.png的图库。 我想从文件夹库中选择图片。

您知道如何修改代码吗?

我用过:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.gallery", "com.android.camera.GalleryPicker"));

//   intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

Log.i("aa","adafdsfa");
startActivityForResult(intent, 1);

通过我获取文件夹库,但我无法获得图片路径。

1 个答案:

答案 0 :(得分:1)

 File dir = new File(Environment.getExternalStorageDirectory().toString() + "/sdcard/yourfolder");
        Log.d("File path ", dir.getPath());
        String dirPath=dir.getAbsolutePath();
        if(dir.exists() && dir.isDirectory()) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            // tells your intent to get the contents
            // opens the URI for your image directory on your sdcard
                            //its upto you what data you want image or video.
            intent.setType("image/*");
        //  intent.setType("video/*");
            intent.setData(Uri.fromFile(dir));
        //  intent.setType("media/*");
        //  intent.
            startActivityForResult(intent, 1);
        }
        else
        {
            showToast("No file exist to show");
        }   


      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == 1) {
         if (data==null) {
            showToast("No image selected");
            //finish();
        }
         else
         {
         Uri selectedImageUri = data.getData();

      //  String filemanagerstring = selectedImageUri.getPath();

         //MEDIA GALLERY
       String  selectedImagePath = getPath(selectedImageUri);

         if(selectedImagePath!=null)
         {
             Intent intent = new Intent();
             intent.setAction(Intent.ACTION_VIEW);
             intent.setData(selectedImageUri);
             startActivity(intent);
         }

         else
         {
             showToast("Image path not correct");
         }


     }
        }

}