【问题标题】:Select PDF from sdcard in Android在 Android 中从 sdcard 中选择 PDF
【发布时间】:2016-12-13 18:04:20
【问题描述】:

我想从 sdcard 中选择 PDF 文件并上传到我的 android 项目中的服务器。请帮我这样做。如果可能的话,请告诉我这样做的代码。我也尝试使用视频选择示例代码,但它显示“No application can perform this Action”。 enter code here

Button b1;
private static final int SELECT_VIDEO_DIALOG = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1=(Button)findViewById(R.id.button1);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setType("pdf/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Video"), SELECT_VIDEO_DIALOG);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK)
    {
        if (requestCode == SELECT_VIDEO_DIALOG)
        {
            System.out.println("SELECT_VIDEO");
            Uri selectedImageUri = data.getData();
            String selectedpath = getPath(selectedImageUri);
            System.out.println("SELECT_VIDEO Path : " + selectedpath);
        }
    }
}

private String getPath(Uri uri) {
    String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION}; 
    @SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    cursor.moveToFirst(); 
    String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
    int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
    long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
    //some extra potentially useful data to help with filtering if necessary
    System.out.println("size: " + fileSize);
    System.out.println("path: " + filePath);
    System.out.println("duration: " + duration);

    return filePath;
}

【问题讨论】:

  • 你在什么时候看到No application can perform this Action?您在 onClick 处理程序中的代码对我有用。
  • 我在单击从 sdcard 中选择 pdf 的按钮时收到该消息。
  • 那么现在你知道原因了。您没有合适的应用程序来选择设备上安装的文件。从安装文件浏览器/管理器开始。
  • 我想为此做什么?我们可以从 sdcard 中获取视频文件,而无需使用任何 3rd 方应用程序。那为什么我们要使用另一个应用程序从 sdcard 中选择 pdf 文件?
  • 你自己想要它。 onClick 中的代码使用另一个应用程序来选择文件。

标签: android


【解决方案1】:

您好,如果您想列出文件而不是可以使用

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("file/*");
    startActivityForResult(intent, YOUR_RESULT_CODE);

如果您只想要 PDF 文件,请点击此链接 https://stackoverflow.com/a/27407812/4741746

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多