需要了解android中的app chooser

时间:2014-05-20 12:08:41

标签: android listview view picker android-external-storage

我在listview中有一个文件列表..这些文件实际上驻留在SD卡上。现在我想使用应用程序选择器打开这些文件。即如果此文件是图像,它应该显示我的手机中可以在应用程序选择器框中打开jpg类型文件的所有应用程序。 我怎么能这样做..有人可以给我任何想法吗? 任何帮助表示赞赏:) 提前致谢 我发现了一段代码..我可以在我的应用程序中使用它吗?

public class Redirector {
public static void showActivityWithChooser( Context context, int chooserLabelTitleId, Intent intent ) {
  try {
    context.startActivity( Intent.createChooser( intent, 
                 context.getResources().getString( chooserLabelTitleId )) );
  } catch( Exception e ) {
    e.printStackTrace();
  }
}

public static void viewInExternalApplication( Context context, String url ) {
  Intent intent = new Intent(   Intent.ACTION_VIEW );
  intent.setData( Uri.parse( url ) );
  intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET );
  showActivityWithChooser( context, R.string.open_chooser_title, intent );
}

}

5 个答案:

答案 0 :(得分:1)

只需发送意图。 Android会显示一个app chosser本身,如果安装了多个能够显示指定文件类型的应用程序,或直接启动应用程序,如果只有一个(或用户选择"总是" /"记住我的选择"在app chooser中)

答案 1 :(得分:1)

String path = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/YOUR_PATH_TO_Images/";

 try {
            if (f.exists()) {
                File file = new File(path
                        + listViewArray.get(position).getImageName());
                Intent target = new Intent(Intent.ACTION_VIEW);

                target.setDataAndType(Uri.fromFile(file),
                        "image/*");


                target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                Intent intent1 = Intent.createChooser(target,
                        "Open With");

                startActivity(intent1);
            }

        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), "No Pdf found",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

答案 2 :(得分:0)

1.这个问题与Mime type

有关

首先获取文件扩展名并设置type of mime used by element into list

private String extenstionFile(String url) {
    if (url.indexOf("?")>-1) {
        url = url.substring(0,url.indexOf("?"));
    }
    if (url.lastIndexOf(".") == -1) {
        return null;
    } else {
        String ext = url.substring(url.lastIndexOf(".") );
        if (ext.indexOf("%")>-1) {
            ext = ext.substring(0,ext.indexOf("%"));
        }
        if (ext.indexOf("/")>-1) {
            ext = ext.substring(0,ext.indexOf("/"));
        }
        return ext.toLowerCase();

    }
}

然后打开支持的应用程序列表类型: -

MimeTypeMap myMime = MimeTypeMap.getSingleton(); 
Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);

//Intent newIntent = new Intent(Intent.ACTION_VIEW);
   String mimeType =   myMime.getMimeTypeFromExtension(extenstionFile(getFile().toString()).substring(1));
   newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType);
   newIntent.setFlags(newIntent.FLAG_ACTIVITY_NEW_TASK);
try {
    _context.startActivity(newIntent);
} catch (android.content.ActivityNotFoundException e) {
    Toast.makeText(_context, "No handler for this type of file.", 4000).show();
}

答案 3 :(得分:0)

选择文件后,请检查文件是什么(例如pdf,jpg),然后创建ACTION_VIEW意图并根据所选文件类型设置意图类型。现在brocast意图,系统本身将显示支持查看yor文件的应用程序列表。

检查以下链接,您会有更好的想法。

http://developer.android.com/guide/components/intents-filters.html

答案 4 :(得分:0)

要获得可以成功打开给定intent的应用程序,您将使用PackageManager。只需构建上述intent,然后使用此代码获取可以处理intent的应用程序。

Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.addCategory("android.intent.category.LAUNCHER");
myIntent.setType("mp3");    

PackageManager manager = getPackageManager();
        List<ResolveInfo> info = manager.queryIntentActivities(myIntent,PackageManager.MATCH_DEFAULT_ONLY);

这将为您提供有关可以处理意图的程序的所有信息,包括图标和包名。然后,您可以使用这些选项创建一个对话框,并保存用户选择的选项。