【问题标题】:Android Intent chooser - wrong propositionsAndroid Intent 选择器 - 错误的命题
【发布时间】:2016-07-07 07:46:52
【问题描述】:

我想使用意图选择器打开我的应用程序中的任何文件 - 不幸的是,来自选择器的提议......很奇怪。选择png 文件后,它只显示一个选项 - Drive PDF Viewer。但是当我尝试使用默认文件资源管理器打开同一个文件时,它正确地建议使用画廊或照片。

String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.toLowerCase());
Log.i(TAG, mime); // <------------ image/png
Intent intent= new Intent(Intent.ACTION_VIEW);
intent.setType(mime);
intent.setData(Uri.fromFile(file));  // <----- png file
Intent chooser = Intent.createChooser(intent, "Choose an application to open with:");
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooser);

这里有什么问题?

【问题讨论】:

  • 如果改成:intent.setType("image/*");有用吗?

标签: android android-intent gallery filechooser


【解决方案1】:

setDataAndType试试这个:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "image/*");
Intent chooser = Intent.createChooser(intent, "Choose an application to open with:");
context.startActivity(chooser);

请查看 Intent 类中方法 setType(String type) 的 Javadoc:
...
此方法会自动清除之前设置的所有数据(例如通过 setData(Uri))。
...

【讨论】:

    【解决方案2】:

    试试这个,

    String[] mimetypes = {"image/*"};
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
    

    这可能对你有帮助

    【讨论】:

    • 这会打开带有消息的活动:All apps associated with this action have been turned off, blocked, or are not installed... 但我很确定 GALERY 应用程序已安装(它在默认文件资源管理器中工作)
    【解决方案3】:

    尝试添加这个 insted mime

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Image"),REQUEST_TAKE_GALLERY_IMAGE);
    

    【讨论】:

      【解决方案4】:

      当我使用以下代码时,我也看到同样的消息弹出:

      public class MainActivity extends AppCompatActivity {
      intent = new Intent(MainActivity.this, SecondActivity.class);
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.coordinator_main);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
      
          TextView textView = (TextView) findViewById(R.id.gotoA);
          textView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
      
                  intent.putExtra("show 1",true);
                  startActivity(intent);
              }
          });
      
          TextView textView1 = (TextView) findViewById(R.id.gotoB);
          textView1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
      
                  intent.putExtra("show 2",true);
                  startActivity(intent);
              }
          });
      }
      

      所以我将意图构造器带到我使用该意图实例的位置。

      public class MainActivity extends AppCompatActivity {
      Intent intent;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.coordinator_main);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
      
          TextView textView = (TextView) findViewById(R.id.gotoA);
          textView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  intent = new Intent(MainActivity.this, SecondActivity.class);
                  intent.putExtra("show 1",true);
                  startActivity(intent);
              }
          });
      
          TextView textView1 = (TextView) findViewById(R.id.gotoB);
          textView1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  intent = new Intent(MainActivity.this, SecondActivity.class);
                  intent.putExtra("show 2",true);
                  startActivity(intent);
              }
          });
      }
      

      对我来说,这解决了问题。

      【讨论】:

        猜你喜欢
        • 2019-06-25
        • 2011-09-09
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-21
        • 2015-12-21
        • 1970-01-01
        相关资源
        最近更新 更多