找不到处理 Intent 的 Activity:android.intent.action.VIEW

发布于 2024-11-05 04:22:59 字数 2571 浏览 6 评论 0原文

这是我播放录制的音频 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 平板电脑中它工作正常。我该如何解决这个问题?

This is my code to play the recorded audio 3gp file

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

But while running it in my HTC device (Android 2.2 Froyo) shows an exception:

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)

In Galaxy tablet it's working fine. How can I resolve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

2024-11-12 04:22:59

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");

Url addresses must be preceded by http://

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

throws an ActivityNotFoundException. If you prepend "http://", problem solved.

Uri uri = Uri.parse("http://www.google.com");
最单纯的乌龟 2024-11-12 04:22:59

如果您在尝试从 Android 应用程序打开网页时也遇到此错误,那是因为您的网址如下所示:

www.google.com

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

将此代码添加到您的 Activity/Fragment:

 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://http:// 前缀,那么你就可以开始了,否则 if 语句会为你处理这个问题

If you are also getting this error when trying to open a web page from your android app it is because your url looks like this:

www.google.com

instead of:
https://www.google.com or http://www.google.com

add this code to your Activity/Fragment:

 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);
    }
}

just pass your url to openWebPage(). If it is already prefixed with https:// or http:// then you are good to go, else the if statement handles that for you

明天过后 2024-11-12 04:22:59

这是正确的做法。

    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;

This is the right way to do it.

    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();
    }

you need to import import android.webkit.MimeTypeMap;

荒人说梦 2024-11-12 04:22:59

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

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

我遇到了同样的错误 android.content.ActivityNotFoundException:找不到处理 Intent 的 Activity

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

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

For me when trying to open a link :

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

I got the same error android.content.ActivityNotFoundException: No Activity found to handle Intent

The problem was because i didnt have any app that can open URLs (i.e.
browsers) installed in my phone. So after Installing a browser the
problem was solved.

*Lesson : Make sure there is at least one app which handles the intent you are calling *

感情旳空白 2024-11-12 04:22:59

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 }`

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

Answer by Maragues made me check out logcat output. Appears that the path you pass to Uri needs to be prefixed with
file:/

Since it is a local path to your storage.

You may want too look at the path itself too.

`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 }`

Mount point seems to appear twice in the full path.

柠北森屋 2024-11-12 04:22:59

如果您没有传递正确的 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();
    }
}

If you don't pass the correct web URL and don't have a browser, ActivityNotFoundException occurs, so check those requirements or handle the exception explicitly. That can resolve your problem.

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();
    }
}
南薇 2024-11-12 04:22:59

检查这个有用的方法:

URLUtil.guessUrl(urlString)

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

Check this useful method:

URLUtil.guessUrl(urlString)

It makes google.com -> http://google.com

清风挽心 2024-11-12 04:22:59

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

您可以使用 API 的 tangerine 解决方案最多 29:

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 中返回 null 30

This exception can raise when you handle Deep linking or URL for a browser, if there is no default installed. In case of Deep linking there may be no application installed that can process a link in format myapp://mylink.

You can use the tangerine solution for API up to 29:

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()
    }
}

For API >= 30 see intent.resolveActivity returns null in API 30.

ま昔日黯然 2024-11-12 04:22:59

为了避免崩溃,需要检查以下 2 件事

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

Kotlin 的应用程序

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)
       }
    }
}

To avoid crash below 2 things need to be checked

  1. Check if url starts with http or https
  2. Check if device has any app that can handle url

Kotlin

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)
       }
    }
}
虫児飞 2024-11-12 04:22:59

我遇到了同样的问题,所以我正在查看 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);

I had this same issue, so I was looking at the intent which is logged in LogCat. When viewing the video from the Gallery, it called the same Intent.View intent, but set the Type to "audio/*", which fixed my issue, and doesn't require importing MimeTypeMap. Example:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse(path);
intent.setDataAndType(data, "audio/*");
startActivity(intent);
思念满溢 2024-11-12 04:22:59

如果您的 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());
            }

If you have these error in your logcat then use these code it help me

 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());
            }

also

   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());
            }
梦途 2024-11-12 04:22:59

如果这个异常甚至更改为

“音频/*”

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

Uri.fromFile(文件(内容))而不是Uri.parse(路径)

 val intent =Intent(Intent.ACTION_VIEW)
                    intent.setDataAndType(Uri.fromFile(File(content)),"audio/*")
                    startActivity(intent)

Had this exception even changed to

"audio/*"

But thanx to @Stan i have turned very simple but usefully solution:

Uri.fromFile(File(content)) instead Uri.parse(path)

 val intent =Intent(Intent.ACTION_VIEW)
                    intent.setDataAndType(Uri.fromFile(File(content)),"audio/*")
                    startActivity(intent)
神回复 2024-11-12 04:22:59

而不是使用 Try 和 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"))

instead of using Try and catch. You should use this way

val intent = Intent(Intent.ACTION_VIEW)
                    intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                    intent.setDataAndType(uri, type)    
startActivity(Intent.createChooser(intent, "view action"))
诺曦 2024-11-12 04:22:59

我有两个想法:

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

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

I have two ideas:

First: if you want to play 3gp file you should use mime types "audio/3gpp" or "audio/mpeg"

And second: you can try use method setData(data) for intent without any mime type.

白况 2024-11-12 04:22:59

首先在 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);

First try this code inside AndroidManifest

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

If this code works, so fine. But if not. Try this code from which class you want to pass the data or uri.

Intent window = new Intent(getApplicationContext(), Browser.class);
window.putExtra("LINK", "http://www.yourwebsite.com");
startActivity(window);
呆橘 2024-11-12 04:22:59

尝试检查任何网址,例如在路径中添加

https://www.google.com

并启动活动
如果它有效,那么您添加了错误的路径

try checking with any Url like add

https://www.google.com

in path and start activity
if its works than you are adding wrong path

千里故人稀 2024-11-12 04:22:59

我遇到了像这个案例这样的问题。就我而言,当我的手机中没有浏览器应用程序时,就会发生这种情况。我安装了一个浏览器应用程序并解决了我的问题。不要忘记将代码放入 Try/Catch 块中。

I had a problem like this case. In my case , it used to happen when I didn't have a browser app in my phone. I installed a browser app and solved my problem. Don't forget to put your code in Try/Catch block.

遥远的绿洲 2024-11-12 04:22:59

致命异常:main(问双子座)
进程:com.example.dryfruits,PID:22051
android.content.ActivityNotFoundException:找不到处理 Intent 的活动 { act=android.intent.action.VIEW dat=upi://pay?pa=dn.ssenterprises-2@okicici&pn=Dilip SHelke&am= 400.0& cu=INR pkg=com.google.android.apps.nbu.paisa.user }

FATAL EXCEPTION: main (Ask Gemini)
Process: com.example.dryfruits, PID: 22051
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=upi://pay?pa=dn.ssenterprises-2@okicici&pn=Dilip SHelke&am= 400.0&cu=INR pkg=com.google.android.apps.nbu.paisa.user }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文