【问题标题】:How can I display a specific folder in Android Gallery3D (cooliris)?如何在 Android Gallery3D (cooliris) 中显示特定文件夹?
【发布时间】:2013-03-13 16:02:51
【问题描述】:

我正在使用Gallery3D (froyo-stable)

我正在尝试制作一个仅在图库视图中显示特定文件夹中的图像的小应用程序。

Uri targetUri = Media.EXTERNAL_CONTENT_URI;
String folderPath = Environment.getExternalStorageDirectory().toString() + "/DCIM/";
int folderBucketId = folderPath.toLowerCase().hashCode();
targetUri = targetUri.buildUpon().appendQueryParameter("bucketId", String.valueOf(folderBucketId)).build();

initializeDataSource()

// Creating the DataSource objects.
final LocalDataSource localDataSource = new LocalDataSource(Gallery.this, targetUri.toString(), false);

但我有错误

"Error finding album " + bucketId);

CacheService.loadMediaSets.:

 Log.e(TAG, "Error finding album " + bucketId);

我该如何解决这个问题? 谢谢你

【问题讨论】:

  • 您想要特定文件夹的路径并从该文件夹中检索图像??
  • 是的,只显示一个文件夹中的所有图像
  • 我不知道画廊 3d。我已经使用渲染脚本使用了 3d carosel。但是,假设文件夹只有图像,您是否需要从单个文件夹中提取图像路径的代码。
  • 假设您需要提取文件夹下的文件路径,您可以使用下面的代码。您还可以考虑使用渲染脚本使用 carousel 3d
  • Raghunandan:感谢您的评论,但我想要的是 @Joe 的回答

标签: android android-gallery mediastore


【解决方案1】:

假设您需要在文件夹下查找文件的路径。

    private String[] mFileStrings;
private File[] listFile;

public void getFromSdcard()
{
    File file=  new File(android.os.Environment.getExternalStorageDirectory(),"Your Folder Name");

        if (file.isDirectory())
        {
            listFile = file.listFiles();
            mFileStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++)
            {
                mFileStrings[i] = listFile[i].getAbsolutePath();
                System.out.println("...................................."+mFileStrings[i]);
            }
        }
}

在我的手机中,我的内存被命名为 sdcard0。所以为了确保你得到正确的路径,你可以使用下面的代码。

 String externalpath = new String();
 String internalpath = new String();

public  void getExternalMounts() {
Runtime runtime = Runtime.getRuntime();
try
{
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;

BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
    if (line.contains("secure")) continue;
    if (line.contains("asec")) continue;

    if (line.contains("fat")) {//external card
        String columns[] = line.split(" ");
        if (columns != null && columns.length > 1) {
            externalpath = externalpath.concat("*" + columns[1] + "\n");
        }
} 
        else if (line.contains("fuse")) {//internal storage
        String columns[] = line.split(" ");
        if (columns != null && columns.length > 1) {
            internalpath = internalpath.concat(columns[1] + "\n");
        }
    }
}
}
catch(Exception e)
{
    e.printStackTrace();
}
 System.out.println("Path  of sd card external............"+externalpath);
 System.out.println("Path  of internal memory............"+internalpath);
}

另一种选择:

您还可以使用 3d 轮播来显示图像,使用渲染脚本作为替代方案。

http://code.google.com/p/android-ui-utils/downloads/detail?name=CarouselExample.zip.

生成的快照。 Carousel 3d 在 Jelly Bean 上进行了测试。

【讨论】:

    【解决方案2】:

    “查找专辑时出错”问题很可能是由于您在代码中获得 bucketId 的“DCIM”文件夹中没有直接包含任何图像。例如,如果您改用“/DCIM/Camera”(假设那里有一些图像),您应该不会收到错误。

    但是,如果我正确理解您想要的内容,我相信您需要对 Gallery3D 代码进行额外修改,以便在您按照此路线启动时使其显示特定文件夹(因为代码只是不是为那样使用而设计的)。

    我相信您可以通过将意图设置为onCreate() 中的特定意图来更轻松地实现您想要的,而不是使用上面的代码:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setIntentToSpecificFolder();
    
        mApp = new App(Gallery.this);
        // :
        // the rest of onCreate() code
        // :
        Log.i(TAG, "onCreate");
    }
    
    private void setIntentToSpecificFolder() {
        String folderPath = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";
        int folderBucketId = folderPath.toLowerCase().hashCode();
        Uri targetUri = Media.EXTERNAL_CONTENT_URI.buildUpon().appendQueryParameter("bucketId", String.valueOf(folderBucketId)).build();
    
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(targetUri, "vnd.android.cursor.dir/image");
        setIntent(intent);
    }
    

    基本上,我们在这里所做的是利用应用程序的ACTION_VIEW 意图处理,当它提供vnd.android.cursor.dir/image MIME 类型时。

    【讨论】:

    • 谢谢,我如何在 API 级别 8 中使用 getContentUri 或类似的东西
    • 我可以使用 Images.Media.EXTERNAL_CONTENT_URI 代替这个吗?
    • 是的,它也应该可以工作。我编辑了答案以改用它。
    • 此解决方案非常适合在使用 Android Gallery 应用程序时显示相册,但它会使 Google 的“照片”应用程序崩溃。有什么办法可以让它对双方都有效吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多