119

这是我播放录制的音频 3gp 文件的代码

 Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
        Uri data = Uri.parse(path);
        intent.setDataAndType(data, "audio/mp3");
        startActivity(intent);

但是在我的 HTC 设备(Android 2.2 Froyo)上运行它时显示异常:

05-04 16:37:37.597: WARN/System.err(4065): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/mnt/sdcard/mnt/sdcard/audio-android.3gp typ=audio/mp3 }
05-04 16:37:37.597: WARN/System.err(4065):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1567)
05-04 16:37:37.597: INFO/ActivityManager(92): Starting activity: Intent { act=android.intent.action.VIEW dat=/mnt/sdcard/mnt/sdcard/audio-android.3gp typ=audio/mp3 }
05-04 16:37:37.607: WARN/System.err(4065):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1537)
05-04 16:37:37.607: WARN/System.err(4065):     at android.app.Activity.startActivityForResult(Activity.java:2858)
05-04 16:37:37.607: WARN/System.err(4065):     at android.app.Activity.startActivity(Activity.java:2964)
05-04 16:37:37.607: WARN/System.err(4065):     at com.ey.camera.AudioRecorder.playAudio(AudioRecorder.java:244)
05-04 16:37:37.607: WARN/System.err(4065):     at com.ey.camera.AudioRecorder$4.onClick(AudioRecorder.java:225)
05-04 16:37:37.607: WARN/System.err(4065):     at android.view.View.performClick(View.java:2408)
05-04 16:37:37.607: WARN/System.err(4065):     at android.view.View$PerformClick.run(View.java:8817)
05-04 16:37:37.607: WARN/System.err(4065):     at android.os.Handler.handleCallback(Handler.java:587)
05-04 16:37:37.607: WARN/System.err(4065):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 16:37:37.607: WARN/System.err(4065):     at android.os.Looper.loop(Looper.java:144)
05-04 16:37:37.607: WARN/System.err(4065):     at android.app.ActivityThread.main(ActivityThread.java:4937)
05-04 16:37:37.607: WARN/System.err(4065):     at java.lang.reflect.Method.invokeNative(Native Method)
05-04 16:37:37.607: WARN/System.err(4065):     at java.lang.reflect.Method.invoke(Method.java:521)
05-04 16:37:37.607: WARN/System.err(4065):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-04 16:37:37.607: WARN/System.err(4065):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-04 16:37:37.607: WARN/System.err(4065):     at dalvik.system.NativeStart.main(Native Method)

在 Galaxy 平板电脑中,它运行良好。我该如何解决这个问题?

4

16 回答 16

176

Url 地址必须以 http:// 开头

Uri uri = Uri.parse("www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);

抛出 ActivityNotFoundException。如果你在前面加上“http://”,问题就解决了。

Uri uri = Uri.parse("http://www.google.com");
于 2011-06-03T09:47:02.610 回答
59

如果您在尝试从您的 android 应用程序打开网页时也遇到此错误,那是因为您的 url 看起来像这样:

www.google.com

而不是: https://www.google.comhttp://www.google.com

将此代码添加到您的活动/片段:

 public void openWebPage(String url) {

    Uri webpage = Uri.parse(url);

    if (!url.startsWith("http://") && !url.startsWith("https://")) {
        webpage = Uri.parse("http://" + url);
    }

    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

只需将您的网址传递给openWebPage(). 如果它已经以https://or为前缀,http://那么你很高兴,否则 if 语句会为你处理

于 2017-01-25T10:49:22.487 回答
33

这是正确的做法。

    try
    {
    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File(aFile.getAbsolutePath()); 
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(myIntent);
    }
    catch (Exception e) 
    {
        // TODO: handle exception
        String data = e.getMessage();
    }

你需要导入 import android.webkit.MimeTypeMap;

于 2011-06-20T09:58:18.003 回答
23

对我来说,尝试打开链接时:

Uri uri = Uri.parse("https://www.facebook.com/abc/");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);

我得到了同样的错误 android.content.ActivityNotFoundException: No Activity found to handle Intent

问题是因为我的手机中没有安装任何可以打开 URL(即浏览器)的应用程序。所以安装浏览器后问题就解决了。

*课程:确保至少有一个应用程序可以处理您调用的意图*

于 2016-08-02T07:30:24.973 回答
18

Maragues 的回答让我查看了 logcat 输出。看来你传递给 Uri 的路径需要加上前缀
file:/

因为它是您的存储的本地路径。

您可能也想查看路径本身。

`05-04 16:37:37.597: WARN/System.err(4065): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/mnt/sdcard/mnt/sdcard/audio-android.3gp typ=audio/mp3 }`

挂载点似乎在完整路径中出现了两次。

于 2011-08-25T18:26:55.140 回答
11

如果您没有传递正确的 Web URL 并且没有浏览器,则会发生 ActivityNotFoundException,因此请检查这些要求或明确处理异常。那可以解决你的问题。

public static void openWebPage(Context context, String url) {
    try {
        if (!URLUtil.isValidUrl(url)) {
            Toast.makeText(context, " This is not a valid link", Toast.LENGTH_LONG).show();   
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
        }
    } catch (ActivityNotFoundException e) {
        Toast.makeText(context, " You don't have any browser to open web page", Toast.LENGTH_LONG).show();
    }
}
于 2018-03-01T09:15:03.743 回答
10

检查这个有用的方法:

URLUtil.guessUrl(urlString)

它使 google.com -> http://google.com

于 2018-12-19T08:45:31.433 回答
4

我遇到了同样的问题,所以我正在查看登录 LogCat 的意图。从图库中查看视频时,它调用了相同的 Intent.View 意图,但将类型设置为“audio/*”,这解决了我的问题,并且不需要导入 MimeTypeMap。例子:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse(path);
intent.setDataAndType(data, "audio/*");
startActivity(intent);
于 2011-12-19T14:49:28.990 回答
4

如果您的 logcat 中有这些错误,请使用这些代码对我有帮助

 Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(data.getLocation())), "audio/*");
            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.d("error", e.getMessage());
            }

   Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
            intent.setDataAndType(Uri.fromFile(new File(data.getLocation())), "audio/*");
            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.d("error", e.getMessage());
            }
于 2018-12-15T09:28:47.547 回答
4

Deep linking如果未安装默认值,则在您处理浏览器的 URL时可能会引发此异常。在深度链接的情况下,可能没有安装可以处理格式链接的应用程序myapp://mylink

您可以将tangerine解决方案用于最多 29 个 API:

private fun openUrl(url: String) {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    val activityInfo = intent.resolveActivityInfo(packageManager, intent.flags)
    if (activityInfo?.exported == true) {
        startActivity(intent)
    } else {
        Toast.makeText(
            this,
            "No application that can handle this link found",
            Toast.LENGTH_SHORT
        ).show()
    }
}

对于 API >= 30,请参阅intent.resolveActivity 在 API 30 中返回 null

于 2020-06-23T09:07:18.330 回答
3

为避免崩溃低于 2 件事情需要检查

  1. 检查 url 是否以 http 或 https 开头
  2. 检查设备是否有任何可以处理 url 的应用程序

科特林

if (url.startsWith("http") || url.startsWith("https")) {
    Intent(Intent.ACTION_VIEW,Uri.parse(url)).apply{
    // Below condition checks if any app is available to handle intent
    if (resolveActivity(packageManager) != null) {
           startActivity(this)
       }
    }
}
于 2021-08-19T07:46:08.197 回答
3

如果这个例外甚至更改为

“声音的/*”

但是感谢@Stan ,我已经变成了非常简单但有用的解决方案:

Uri.fromFile(File(content))反而Uri.parse(path)

 val intent =Intent(Intent.ACTION_VIEW)
                    intent.setDataAndType(Uri.fromFile(File(content)),"audio/*")
                    startActivity(intent)
于 2018-04-23T14:17:58.570 回答
1

我有两个想法:

第一:如果你想播放 3gp 文件,你应该使用 mime 类型“audio/3gpp”或“audio/mpeg”

第二:您可以尝试使用没有任何 mime 类型setData(data)的方法。intent

于 2011-05-04T11:46:55.427 回答
1

首先在AndroidManifest中尝试这段代码

  <application>
       
    <uses-library
        android:name="org.apache.http.legacy"
        android:required="false" />
 </application>

如果此代码有效,那就太好了。但如果没有。试试这个你想从哪个类传递数据或uri的代码。

Intent window = new Intent(getApplicationContext(), Browser.class);
window.putExtra("LINK", "http://www.yourwebsite.com");
startActivity(window);
于 2020-01-10T12:14:19.887 回答
0

而不是使用 Try and catch。你应该使用这种方式

val intent = Intent(Intent.ACTION_VIEW)
                    intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                    intent.setDataAndType(uri, type)    
startActivity(Intent.createChooser(intent, "view action"))
于 2021-06-07T03:52:50.447 回答
0

尝试检查任何网址,如添加

https://www.google.com

在路径中并开始活动,如果它的工作原理比你添加错误的路径

于 2020-07-29T06:54:45.300 回答