标签: android-file

为什么FileOutputStream抛出FileNotFoundException?

Android Developer参考(本页)说:

Throws FileNotFoundException
Run Code Online (Sandbox Code Playgroud)

但在一开始,它说:

打开与此Context的应用程序包关联的私有文件以进行写入.如果文件尚不存在,则创建该文件.

如果是这种情况,为什么会抛出FileNotFoundException?

我只是想确保我正确处理所有案件.我正在使用默认功能,所以我可以将它包装在try..catch块中没有任何内容的catch块中,因为它不可能FileNotFoundException被抛出默认功能吗?

编辑: '默认功能'的示例:

String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
Run Code Online (Sandbox Code Playgroud)

java android android-context java-io android-file

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

getExternalFilesdir失败

OnActivityCreated我在做:

activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Run Code Online (Sandbox Code Playgroud)

在logcat我得到:

com.package W/ContextImpl? Failed to ensure directory: /storage/sdcard1/Android/data/com.package/files/Pictures
Run Code Online (Sandbox Code Playgroud)

这种情况只发生在LOLLIPOP(MOTO G 2014)上,KITKAT(Nexus 4)的一切都很好.我在清单上有WRITE_EXTERNAL_STORAGE.我在这里错过了什么?存储可通过文件浏览应用程序(如ES Explorer)安装和访问.

编辑:令人惊讶的是,即使我收到上面报告的警告,文件也会在目录下正确创建.

android android-file

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

从 URI 转换为 Uri,反之亦然

将 Uri (android) 对象转换为 URI (java.net) 对象(反之亦然)的最佳方法是什么?我使用以下方法将 Uri 转换为 URI:

Uri androidUri;
URI netURI= new URI(androidUri.toString());
Run Code Online (Sandbox Code Playgroud)

但我不知道这是否是最好的方法,也不知道如何扭转它。

android uri android-file

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

Android从Google云端硬盘获取Uri路径

我有这个代码将文件上传到我的应用程序,当用文件管理器,dropbox或其他任何东西打开文件时,返回的路径是正确的,我可以访问它,我只是遇到Google Drive问题,它返回一些以"exposed_content"开头的路径,我不能以任何方式"解码"它,我搜索过并没有找到办法,任何人都有任何想法?

if (resultCode == Activity.RESULT_OK) {
            if ((data != null) && (data.getData() != null)) {
                final Uri filePath;
                if (data.getDataString().startsWith("content")) {
                    filePath = getRealPathFromURI(getApplicationContext(), data.getData());
                } else {
                    filePath = data.getData();
                }
                // TODO bug with google drive
                if (filePath.getLastPathSegment() != null) {
                    tvSelectedFile.setText("File selected: " + filePath.getLastPathSegment());

                } else {
                    tvSelectedFile.setText("File can not be accessed, please try another way");
                }

            }
}
Run Code Online (Sandbox Code Playgroud)

android android-file google-drive-api

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

在Lollipop上的Android图库中显示图像文件

我正在尝试制作一些在应用程序中可见的图片(因此可以在应用程序外部共享和查看),但我想将图像保留在应用程序的数据目录中,以便应用程序被删除后会被删除(因此它们存储在"{sdcard}/Android/data/{appID}/Pictures/{subfolder} /"中).

我已经查看了很多答案,对于旧版本的Android,以下两种解决方案都有效,但它们似乎不适用于Lollipop:

MediaScannerConnection.scanFile(this,
    new String[]{file.toString()}, new String[] { "image/jpeg" },
    new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
            Log.i("ExternalStorage", "Scanned " + path + ":");
            Log.i("ExternalStorage", "-> uri=" + uri);
        }
    });

Uri contentUri = Uri.fromFile(file);
Intent mediaScanIntent = new 
    Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri);
sendBroadcast(mediaScanIntent);
Run Code Online (Sandbox Code Playgroud)

我在Nexus 4(Android 5.0.1)和Nexus 6(Android 5.1)中对此进行了测试,但它不起作用.在Nexus 7(Android 4.2.2)中,它确实有效.所有3个设备在{sdcard}/Android/data /文件夹中都有一个".onmedia"文件,因此没有正常的媒体扫描应该选择它们...

有没有人知道如何在所有设备/ Android版本上使用它?这应该是因为noMedia文件首先不起作用并且Lollipop是唯一正确执行此操作的版本吗?

编辑:请注意,即使重新启动手机(也应该触发重新扫描文件)也不起作用.并且有充分的理由因为文件位于".nomedia"目录的子文件夹中...所以任何包含MEDIA_MOUNTED广播的解决方案都是无用的我认为.

android android-gallery android-file android-mediascanner android-5.0-lollipop

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

在android中使用默认文件管理器打开文件夹内容

我正在我的 android 应用程序中处理 pdf 文件。在警报对话框中按“是”应该会在默认文件管理器应用程序中打开 pdf 文件位置。

我试过这个,但它总是打开一个选择器。即使它(ES文件资源管理器)打开“选择路径”对话框而不是应用程序中的文件夹作为访问文件夹的正常方式。

那么,是否可以在“默认文件管理器”中直接打开文件夹的内容?或者可以使用选择器打开,在选择 pdf 文件时,它应该从所选的应用程序中显示 pdf 文件选择器?。

以下是我为实现这一目标所做的尝试。

public void openFolder(String filePath)
    {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    Uri uri = Uri.parse(filePath.substring(0, filePath.lastIndexOf("/")));
    intent.setDataAndType(uri, "*/*");//tried with application/pdf and file/*
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> apps = 
        pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (apps.size() > 0)
    startActivity(intent);//Intent.createChooser(intent, "Open folder"
    }
Run Code Online (Sandbox Code Playgroud)

编辑 我也试过 ACTION_PICK 。它定向到默认文件管理器的特定文件夹。但是从那里选择文件不会打开选择器(pdf 查看器列表对话框)。

android intentfilter android-intent android-file

8
推荐指数
0
解决办法
8611
查看次数

显示带有声音的通知时出现FileUriExposedException

我的应用程序的用户可以选择在我的应用程序中收到通知时应播放的铃声。

我显示一个铃声选择对话框:

    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);

    startActivityForResult(intent, REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)

并保存结果:

    Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    PreferenceManager.getDefaultSharedPreferences(this).edit().putString(RINGTONE_SP_KEY, uri.toString()).apply();
Run Code Online (Sandbox Code Playgroud)

显示通知时,我使用用户选择的铃声或默认铃声:

private void showNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSound(getRingtone());
    builder.setContentText("MyContentText");
    builder.setSmallIcon(R.drawable.test);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());
}

private Uri getRingtone() {

    String ringtoneUriFromSp = PreferenceManager.getDefaultSharedPreferences(this).getString(RINGTONE_SP_KEY, null);

    if (ringtoneUriFromSp == null) {
        return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    } else {
        return Uri.parse(ringtoneUriFromSp);
    }
}
Run Code Online (Sandbox Code Playgroud)

默认和自定义选定的铃声Uri通常都看起来像这样:

content://media/internal/audio/media/16
Run Code Online (Sandbox Code Playgroud)

一切都很好。

我确实(很少)通过Crashlytics 收到以下异常

致命异常:android.os.FileUriExposedException file:///storage/emulated/0/MIUI/ringtone/CloudMagic_3.ogg通过Notification.sound在应用程序之外公开

适用于某些设备(例如Xiaomi Mi …

android android-notifications android-file

8
推荐指数
0
解决办法
979
查看次数

Gmail 错误“无法附加文件”

我正在尝试使用 Gmail 通过电子邮件发送 PDF 文件。但是,Gmail 应用程序显示吐司:

无法附加文件

PDF 文件未损坏,并在应用程序的缓存目录中成功生成。

代码:(如果您需要 Java 代码,请在下面评论。)

    val photoURI: Uri = FileProvider.getUriForFile(this, "com.packagename.provider",
            File(this.cacheDir.path + "/Report.pdf"))

    val emailIntent = Intent(Intent.ACTION_SENDTO)
    emailIntent.data = Uri.parse("mailto:")
    emailIntent.putExtra(Intent.EXTRA_STREAM, photoURI)
    emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("some@domain.com"))
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject")
    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    startActivity(emailIntent)
Run Code Online (Sandbox Code Playgroud)

请帮忙

android file-sharing android-file kotlin android-fileprovider

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

如何创建文件夹(Android R - Api 30)?

了解到 Android 11 有范围存储,但我找不到任何信息,如何在 中创建和使用文件夹/storage/emulated/0/?旧方法仅适用于 api 29 及以下版本:(

android android-file android-storage android-11

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

getFilesDir() 与 getCacheDir()

在 Android 中,您可以使用 getFilesDir() 获取内部 /files/ 文件夹的路径,而 getCacheDir() 获取内部 /cache/ 文件夹的路径。

这两个文件夹之间唯一的功能区别是Android可能会在需要时自动清除缓存目录中的某些文件吗?

我试图了解在哪些情况下使用缓存文件夹是个好主意。

android caching android-file

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