我使用此代码将位图保存在外部存储中但如果它不存在则不会创建该文件夹:
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
try {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
}
Run Code Online (Sandbox Code Playgroud)
如果不存在,如何将图像保存在新目录中,如果设备中有文件夹,则保存默认值?
大多数新的Android设备都有内部SD卡和外部SD卡.我想制作一个文件浏览器应用程序,但我无法找到如何获取在我的应用程序中使用的路径,因为
File file = Environment.getExternalStorageDirectory();
Run Code Online (Sandbox Code Playgroud)
只是在大多数设备中返回,/mnt/sdcard
但是另一个外部SD卡的路径就像/storage1 or /storage2
.任何帮助赞赏.
我需要在我的应用程序中使用全局权限在myapp/files/subdir下创建文件.我这样做是因为我使用外部应用程序来打开一些文件
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
Run Code Online (Sandbox Code Playgroud)
仅在files文件夹下创建文件.运用
File dir=new File(Constants.TASK_DIRECTORY);
dir.mkdirs();
File file=new File(dir, FILENAME);
file.createNewFile(); FileOutputStream fos=new FileOutputStream(file);
Run Code Online (Sandbox Code Playgroud)
在子目录下创建文件但具有私有权限.我需要找到一种方法来组合这两者,以便在子目录中创建一个世界可读的文件
我一直在尝试很多事情,但没有人帮助我,这是我未解决的最长时间问题
我正在尝试在我的Android项目中实现文件选择器.到目前为止我能做的是:
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, PICKFILE_RESULT_CODE);
Run Code Online (Sandbox Code Playgroud)
然后在我的 onActivityResult()
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==-1){
Uri uri = data.getData();
String filePath = uri.getPath();
Toast.makeText(getActivity(), filePath,
Toast.LENGTH_LONG).show();
}
break;
}
Run Code Online (Sandbox Code Playgroud)
这是打开文件选择器,但它不是我想要的.例如,我想选择一个文件(.txt),然后获取它File然后使用它.有了这段代码,我想我会得到完整的路径,但它不会发生; 例如,我得到:/document/5318/.但是通过这条路径我无法获取文件.我创建了一个名为方法PathToFile()返回一个File:
private File PathToFile(String path) {
File tempFileToUpload;
tempFileToUpload = new File(path);
return tempFileToUpload;
}
Run Code Online (Sandbox Code Playgroud)
我正在试图做的是让用户选择File从任何地方表示DropBox,Drive,SDCard,Mega,等...我没有找到正确方式做到这一点,我试图让Path然后得到一个File本Path …
android android-intent android-file android-implicit-intent android-fileprovider
以下代码包括从服务器下载文件并将其保存在存储中,当设备具有内部存储时可以正常工作.
但是当我使用没有内部存储的设备尝试它时,只有外部存储器,我得到以下异常.
java.io.filenotfoundexception打开失败的eacces(权限被拒绝)
public void downloadFile(String dlUrl, String dlName) {
int count;
HttpURLConnection con = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL( dlUrl );
con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.connect();
is = url.openStream();
String dir = Environment.getExternalStorageDirectory() + Util.DL_DIRECTORY;
File file = new File( dir );
if( !file.exists() ){
file.mkdir();
}
Util.LOG_W(TAG, "Downloading: " + dlName + " ...");
fos = new FileOutputStream(file + "/" + dlName);
byte data[] = new …Run Code Online (Sandbox Code Playgroud) app无法在app拥有的目录中的外部存储folder/file上创建android 5(HTC HTC6525LVW os version: 5.0.1).
父文件夹由[getExternalFilesDirs(String type)][1]方法返回.
Sdcard 安装.
有其他人有这个问题或建议如何解决?
(不幸的是我没有这个设备来测试它)
编辑:从一个用户我知道,在此错误之前,她加密了SD卡,然后对其进行了格式化.
android android-file android-permissions android-5.0-lollipop
一开始,用户可以使用新的存储访问框架选择文件(假设应用程序是API> 19):
https://developer.android.com/guide/topics/providers/document-provider.html
然后通过保存URI来保存对所选文件的引用,如下所示:
content://com.android.providers.downloads.documments/document/745
Run Code Online (Sandbox Code Playgroud)
(在这种情况下,文件来自默认下载dir`).
稍后,我想让用户打开这些文件(例如,他们的名字显示在UI列表中,用户选择一个).
我想用Android着名的意图选择器功能做到这一点,我所拥有的只是上面的URI对象......
谢谢,
android android-intent android-file storage-access-framework android-afilechooser
我正在使用文件提供程序将照片保存到给定目的地.我明白了:
java.lang.IllegalArgumentException:尝试打开活动以从摄像头捕获图像时缺少android.support.FILE_PROVIDER_PATHS元数据.
我的manifest.xml文件:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
我的paths.xml文件:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="content" path="Android/data/com.my_package_name/files/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
和Java代码:
File externalFilesDirectory = this.getExternalFilesDir(null);
File imageFile = File.createTempFile(
imageFileName,
".jpg",
externalFilesDirectory
);
Uri photoURI = FileProvider.getUriForFile(this, "com.example", imageFile);
Run Code Online (Sandbox Code Playgroud)
最后一行给出了例外.我在这里错过了什么?我已经关注了官方Android开发网站的教程(https://developer.android.com/training/camera/photobasics.html)
android android-image android-xml android-file android-fileprovider
我的应用程序将所有用户数据和首选项存储在SQLite数据库中,如果用户获得新手机,重新安装或恢复出厂设置,我希望将其保留.我已经在Android的数据备份指南和他们的Android备份服务中做了一些阅读,但在我开始之前仍然有一些问题.
dataChanged()然后在BackupManager调用之前再次发生相同的事情onBackup(),那么这些数据更改将如何处理?BackupManager是否只备份最新版本的数据库(应该包括两个更改)? FileBackupHelper,因为它看起来很简单.至少有一个开发人员似乎通过禁用Write Ahead Logging解决了至少一些兼容性问题.如果我采用这种方法,我可以期待什么样的失败率?如果非常高,我可能会考虑转换为CSV文件并在此过程中返回. 要确保备份代理不会在活动的同时读取或写入文件,每次执行读取或写入时都必须使用synchronized语句.
synchronized语句中对每个数据库进行读/写操作?即,我必须将此添加到我的活动从数据库加载或写入信息的每个地方?那是很多地方.synchronized?谢谢您的帮助.我只是想做到这一点,让我的用户在今年圣诞节获得新手机时感到高兴!
我试图将文件从SD卡转换为Base64,但似乎文件太大,我得到一个OutOfMemoryError.
这是我的代码:
InputStream inputStream = null;//You can get an inputStream using any IO API
inputStream = new FileInputStream(file.getAbsolutePath());
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
attachedFile = Base64.encodeToString(bytes, Base64.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
有没有办法在归档String attachedFile时绕过OutOfMemoryError?
android ×10
android-file ×10
file ×2
android-xml ×1
base64 ×1
database ×1
java-io ×1
save ×1
sqlite ×1
storage ×1