gpt4 book ai didi

Android 应用程序检查是否安装了应用程序来读取不同类型的文件(如 pdf、docx、pptx、txt、rtf)等

转载 作者:行者123 更新时间:2023-11-30 02:11:39 24 4
gpt4 key购买 nike

此代码给出的输出显示 .doc、.tyyt、.txt 阅读器不存在,PDF 阅读器存在。

但实际上在手机中所有阅读器(pdf、txt、doc)都存在,显然没有处理 .tyyt 格式的应用程序。

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import java.io.File;
import android.content.ContentProvider;
public class MainActivity extends ActionBarActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
final EditText t1=(EditText)findViewById(R.id.textView1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PackageManager pm3=getPackageManager();
String msg=" ",type;
PackageManager pm2=getPackageManager();
MimeTypeMap mime2= MimeTypeMap.getSingleton();
String type2=mime2.getMimeTypeFromExtension(".doc");
Intent intent2=new Intent();
intent2.setType(type2);
if(pm2.queryIntentActivities(intent2,PackageManager.MATCH_DEFAULT_ONLY).size()>0)
{
msg+="doc reader present";
t1.setText(msg);

}
else
{
msg+="doc reader absent";
t1.setText(msg);
}


MimeTypeMap mime3= MimeTypeMap.getSingleton();
// I am just trying to check if it works by setting an unkonwn file type,obiviously it should not be present
String type3=mime3.getExtensionFromMimeType(".tyyt");
Intent intent3=new Intent();
intent3.setType(type3);
if(pm3.queryBroadcastReceivers(intent3,PackageManager.MATCH_DEFAULT_ONLY).size()>0)
{
msg+="random reader present";
t1.setText(msg);

}
else
{
msg+="random reader absent";
t1.setText(msg);
}



PackageManager pm1=getPackageManager();
MimeTypeMap mime1= MimeTypeMap.getSingleton();
String type1=mime1.getMimeTypeFromExtension(".txt");
Intent intent1=new Intent();
intent1.setType(type1);
if(pm1.queryIntentActivities(intent1,PackageManager.MATCH_DEFAULT_ONLY).size()>0)
{
msg+="txt reader present";
t1.setText(msg);

}
else {
msg+="txt reader absent";
t1.setText(msg);
}


Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
PackageManager pm=getPackageManager();
MimeTypeMap mime = MimeTypeMap.getSingleton();
type=mime.getMimeTypeFromExtension(".pdf");
intent.setType(type);
if(pm.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY).size()>0)
{
msg+="pdf reader present";
t1.setText(msg);

}
else
{
msg+="pdf reader absent";
t1.setText(msg);
}







}
});


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

这是布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button1"
android:layout_marginTop="83dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Receive"
android:id="@+id/textView1"
android:layout_below="@+id/button1"
android:layout_alignLeft="@+id/button1"
android:layout_alignStart="@+id/button1" />
</RelativeLayout>

这是我的 list 文件。

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shiva.com.opendefaultapp" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:debuggable="true"
</application>

最佳答案

没有操作字符串的隐式 Intent 几乎不匹配。如果您尝试查看这些文件,请使用 ACTION_VIEW Intent。您正在使用 ACTION_VIEW 的唯一一个是 PDF 版本,这就是它起作用的原因。

关于Android 应用程序检查是否安装了应用程序来读取不同类型的文件(如 pdf、docx、pptx、txt、rtf)等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com