【问题标题】:How to get the absolute path of a file stored in Sdcard - Android如何获取存储在 Sdcard 中的文件的绝对路径 - Android
【发布时间】:2014-10-13 06:54:48
【问题描述】:

我正在尝试使用意图发送彩信。

这是我的代码:

private void sendMmsUsingIntent() throws Exception
{
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
    sendIntent.putExtra("address", "1213123123");
    sendIntent.putExtra("sms_body", "some text"); 
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/DCIM/Camera/logo.png"));
    sendIntent.setType("image/png");

    //startActivity(sendIntent);
    startActivity(Intent.createChooser(sendIntent,"MMS"));                        
}

在上面的代码中,我已经硬编码了一个 file name(logo.png) ,我想删除这个依赖关系,谁能帮我解决这个问题?

我想改变它,就像在 sdcard 卡中的某个位置一样,无论可用的媒体文件是什么,我都可以一次随机选择一个。`

我跟着下面一个但没能成功

'How to display files on the SD card in a ListView?'

谢谢

【问题讨论】:

    标签: android android-intent android-sdcard


    【解决方案1】:

    我不确定您到底需要什么,但这里有几个选项。

    1) 如果您需要外部目录文件夹,请使用 EnvironmentgetExternalStorageDirectory() 方法。

    2) 如果您需要默认的 ImageDirectory,则使用 EnvironmentgetExternalStoragePublicDirectory() 方法并将 DIRECTORY_DCIM 作为参数传递

    查看http://developer.android.com/reference/android/os/Environment.html

    编辑:

    获得外部目录的路径后,请执行以下操作:

    File f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File[] allFiles=   f.listFiles(); //All files and folder
    for (int i = 0; i < allFiles.length; i++) {
          if (allFiles[i].isFile()) {
            //Do what you want
          } 
        }
    

    【讨论】:

    • 我想要的是用通用代码替换这个代码 "Uri.parse("file:///sdcard/DCIM/Camera/logo.png"))" 。我的意思不是囤积“logo.png”,我想一张一张地获取 /sdcard/DCIM/Camera 中的其余图像
    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多