相关疑难解决方法(0)

如何检查某个活动是否可以处理意图?

到目前为止,我有这种方法,但它似乎缺少了一些东西

例如,我有一个文件/sdcard/sound.3ga返回false(就像没有可以处理这种类型文件的活动),但当我从文件管理器打开它时,它打开媒体播放器没有问题

我认为这个意图并不完整,我需要更多的东西让我自己确定只有没有可以处理这个意图的活动时,handlerExists变量才会为false

PackageManager pm = getPackageManager();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uriString)).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent.setDataAndType(Uri.fromFile(new File(uriString)),mimetype);
boolean handlerExists = intent.resolveActivity(pm) != null;
Run Code Online (Sandbox Code Playgroud)

android file-type intentfilter android-intent mime-types

105
推荐指数
4
解决办法
5万
查看次数

无法在外部应用程序中打开PDF文件

我想在用户点击按钮时打开PDF文件.目前,我正在使用此代码来实现此目的:

Uri path = Uri.fromFile(new File("file:///android_asset/regola11_1.pdf"));
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(path, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(pdfIntent);
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

当我选择使用Adobe Acrobat时,我会收到一条显示为Toast的消息

"This file could not be accessed Check the location or the network and try again."
Run Code Online (Sandbox Code Playgroud)

当我尝试使用Drive PDF Viewer时,我明白了

"Cannot display PDF ( regola11_1.pdf cannot be opened)"
Run Code Online (Sandbox Code Playgroud)

PDF文件存储在

app > build > intermediates > assets
Run Code Online (Sandbox Code Playgroud)

问题出在哪儿?

编辑

现在我使用以下代码:

File file = new File("\"file:///android_asset/regola11_1.pdf");
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                context.startActivity(intent);
            }
            catch (ActivityNotFoundException e) { …
Run Code Online (Sandbox Code Playgroud)

pdf android uri assets

11
推荐指数
1
解决办法
2万
查看次数

如何在android中共享*.txt文件

我尝试了很多方法,但我不能这样做.

我有一个*.txt文件.我希望通过它分享Bluetooth , wifi , email and ....

当我使用此代码时,我无法共享该文件:

  File file = new File(Environment.getExternalStorageDirectory(), "Email-Ghap/Emails.txt");
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("*/txt");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, file);
        startActivity(Intent.createChooser(sharingIntent, "share file with"));
Run Code Online (Sandbox Code Playgroud)

我完全想要这样:当用户点击共享按钮并选择一个电子邮件发件人(如Gmail)进行共享时.该文件必须附加新文件的文件...

我找到了这个链接/sf/answers/1122537461/

但它共享txt文件内容.我想共享文件而不是txt文件的内容

android android-file android-sharing

5
推荐指数
1
解决办法
5039
查看次数

通过Intent从内部存储共享文本文件

我创建了一堆文本文件,就像下面的代码一样。我将这些文件保存在内部存储器中。我现在的目标是在ListView中显示它,如果用户单击某个项目,它应该打开或通过意图(例如电子邮件)发送文件。

static void createFile (Context context) {
    mTimeStamp = String.valueOf(System.currentTimeMillis()/1000);
    try {
        String fileName = "File_" + mTimeStamp + ".txt";
        mContext = context;
        mOutputStreamWriter = new 
        OutputStreamWriter(context.openFileOutput(fileName ,Context.MODE_PRIVATE));
        String path = mContext.getFilesDir().getAbsolutePath();
        Toast.makeText(mContext, "Creating file: " +  path, 
        Toast.LENGTH_LONG).show();

    } catch (FileNotFoundException e) {
        Log.e(TAG,"Error with creating file writer...");
        e.printStackTrace();
    }
}

static void writeToFileData(String dataToSave) {
    try {
        Toast.makeText(mContext, "Saving data to file: " +  dataToSave, Toast.LENGTH_SHORT).show();
        mOutputStreamWriter.write(dataToSave);
    } catch (IOException e) {
        Log.e(TAG,"Error with writing to file...");
        e.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)

android android-intent internal-storage android-internal-storage

1
推荐指数
1
解决办法
1994
查看次数

如何在android中共享pdf文件?

我尝试从 android/data/mypackage/files/file.pdf 共享 pdf 文件我也在这个应用程序中生成这些 pdf,当我尝试共享它时,pdf 没有出现在电子邮件的附件中,或者谷歌驱动器说:“没有数据共享”。这是我分享pdf的代码:

            val aName = intent.getStringExtra("iName")
            val file = File(this.getExternalFilesDir(null)?.absolutePath.toString(), "$aName")
            val shareIntent = Intent(Intent.ACTION_SEND)
            shareIntent.putExtra(Intent.EXTRA_STREAM,  file)
            shareIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
            shareIntent.type = "application/pdf"
            startActivity(Intent.createChooser(shareIntent, "share.."))
            Toast.makeText(this,"$file",Toast.LENGTH_SHORT).show()
Run Code Online (Sandbox Code Playgroud)

当我敬酒时,pdf 路径看起来是正确的: SS

pdf android share kotlin android-implicit-intent

1
推荐指数
1
解决办法
1579
查看次数