2017-10-06 117 views
0

说我想上传图片或文本文件 当我点击按钮选择文件 我如何使它显示一堆应用程序弹出可以从选择看文件,并选择对上传的这张图片enter image description hereAndroid Java文件选择器选择应用程序来选择文件

我现在有这个

public void sendFile(View view) { 
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 
    intent.setType("*/*"); 
    startActivityForResult(intent, READ_REQUEST_CODE); 
} 

但是这会直接对文档和没有让我选择像画廊的应用或MEGA

关于如何实现这个的任何想法?

+1

,因为你可能已经设置'xxx'默认应用程序 –

回答

0
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class AndroidPick_a_File extends Activity { 

TextView textFile; 

private static final int PICKFILE_RESULT_CODE = 1; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button buttonPick = (Button)findViewById(R.id.buttonpick); 
     textFile = (TextView)findViewById(R.id.textfile); 

     buttonPick.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

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

    }}); 
    } 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    switch(requestCode){ 
    case PICKFILE_RESULT_CODE: 
    if(resultCode==RESULT_OK){ 
    String FilePath = data.getData().getPath(); 
    textFile.setText(FilePath); 
    } 
    break; 

    } 
} 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

<Button 
    android:id="@+id/buttonpick" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="- PICK a file -" 
    /> 
<TextView 
    android:id="@+id/textfile" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

enter image description here

+0

啊哈Intent.ACTION_GET_CONTENT给我的方式更多的选择不错 不过我会怎样得到我张贴的图片中的弹出框 –

+0

是的,它会列出支持此操作的所有安装的应用程序。 –

+0

这不是应该如何完成的。检查下面的答案。我们不再在android 2.3中了。经验必须统一,并且是用户期望的 – Sucho

0

你要明白,文件选择,必须通过Android系统的文件选择器来完成。其他应用程序,如驱动器和Dropbox将在左侧的导航抽屉中收听。兆应该有它的代码,以便它的内容是在Android系统的文件选取器中可见。这就是应该如此。 Android使文件拣选过程统一。

尽管共享文件并非如此。在这种情况下,你会得到选项从喜欢的截图,选择您发布

enter image description here