【问题标题】:how to get path of android galley folder dynamically?如何动态获取android画廊文件夹的路径?
【发布时间】:2015-04-22 04:41:23
【问题描述】:

我想获取 android 相机库文件夹路径以在其中保存图像

在不同的手机中是不同的,例如在银河系中

/sdcard/DCIM/camera/image.jpg

例如在其他手机中

/sdcard/media/images/image.jpg

动态获取画廊文件夹的路径?

任何帮助将不胜感激。

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用以下文件资源管理器示例,该示例显示您手机的所有内容,您将从 galary 中选择您的项目

    FileExplorer.java

      package com.fileUpload;
    
      import java.io.File;
      import java.util.ArrayList;
      import java.util.List;
      import android.app.AlertDialog;
      import android.app.ListActivity;
      import android.content.DialogInterface;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.ArrayAdapter;
      import android.widget.ListView;
      import android.widget.TextView;
    
      public class FileExplorer extends ListActivity {
    
      private List<String> item;
      private List<String> path;
      private String root="/";
      private TextView myPath;
    
      /** Called when the activity is first created. */
      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file);
        myPath = (TextView)findViewById(R.id.path);
        getDir(root);
    }
    
    private void getDir(String dirPath)
    {
     myPath.setText("Location: " + dirPath);
    
     item = new ArrayList<String>();
     path = new ArrayList<String>();
    
     File f = new File(dirPath);
     File[] files = f.listFiles();
    
     if(!dirPath.equals(root))
     {
    
      item.add(root);
      path.add(root);
    
      item.add("../");
      path.add(f.getParent());
    
     }
    
     for(int i=0; i < files.length; i++)
     {
       File file = files[i];
       path.add(file.getPath());
       if(file.isDirectory())
        item.add(file.getName() + "/");
       else
        item.add(file.getName());
     }
    
     ArrayAdapter<String> fileList =
      new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, item);
     setListAdapter(fileList);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    
    File file = new File(path.get(position));
    
      if (file.isDirectory())
      {
        if(file.canRead())
            getDir(path.get(position));
       else
       {
          new AlertDialog.Builder(this)
          .setIcon(R.drawable.icon)
          .setTitle("[" + file.getAbsolutePath() + "] folder can't be read!")
          .setPositiveButton("OK", 
           new DialogInterface.OnClickListener() {
    
              @Override
              public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
              }
             }).show();
         }
       }
       else
       {
           Bundle b=new Bundle();
           b.putString("param1",file.getAbsolutePath());
           Intent i=new Intent(this,DemoActivity.class);
           i.putExtras(b);
           startActivity(i);
    
     }
     }
     }
    

    【讨论】:

    • 我已经尝试过你的代码,但它在 getdir 上给出了 nullpointerexception。
    猜你喜欢
    • 2017-06-10
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多