我使用intent启动默认摄像头,并使用以下路径将这些摄像头图像存储在外部存储中:
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + fileName);
Run Code Online (Sandbox Code Playgroud)
但它没有在画廊中显示.问题出现在nexus 4,7和带有OS 4.4.2的moto G设备上
我试着用
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://" + Environment.getExternalStorageDirectory())));
Run Code Online (Sandbox Code Playgroud)
但它不起作用
我正在尝试将图像文件写入特定目录中的公共图库文件夹,但我一直收到一个错误,我无法打开该文件,因为它是一个目录.
到目前为止我所拥有的是以下内容
//set the file path
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + directory;
File outputFile = new File(path,"testing.png");
outputFile.mkdirs();
FileOutputStream out = new FileOutputStream(outputFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
Run Code Online (Sandbox Code Playgroud)
其中directory是应用程序名称.因此,应用程序保存的所有照片都将进入该文件夹/目录,但我一直收到错误
/storage/sdcard0/Pictures/appname/testing.png: open failed: EISDIR (Is a directory)
Run Code Online (Sandbox Code Playgroud)
即使我不尝试将它放在一个目录中并将变量路径转换为像文件一样
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Run Code Online (Sandbox Code Playgroud)
我没有收到错误,但照片仍未显示在图库中.
***答案问题是,当我最初运行此代码时,它创建了一个名为testing.png的DIRECTORY,因为在创建目录中的文件之前我无法创建目录.因此,解决方案是首先创建目录,然后使用单独的文件写入目录
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.separator + directory;
//directory is a static string variable defined in the class
//make a file with the directory
File outputDir = new File(path);
//create dir if not there
if (!outputDir.exists()) { …Run Code Online (Sandbox Code Playgroud) 我的某些设备有问题。我无法在任何设备上复制它,但是有些用户报告了很多崩溃报告。
这是例外:
java.lang.IllegalArgumentException: Unknown URL content://media/external/file
at android.content.ContentResolver.delete(ContentResolver.java:1024)
Run Code Online (Sandbox Code Playgroud)
我用这个:
context.getContentResolver()。delete(MediaStore.Files.getContentUri(“ external”),MediaStore.Files.FileColumns.DATA +“ =?”,新的String [] {path});
之后,我调用MediaScannerConnection.scanFile()文件的父目录,因为这是如何通知MediaScanner有关文件删除以及如何刷新MTP内容的最有效的方法。我尝试了在stackoverflow上找到的所有其他方式(例如,Android如何使用MediaScannerConnection scanFile),但没有任何一种方法能达到这个目的。
顺便说一句。我仅将其用于API 11及更高版本。肯定安装了外部存储。
我有以下问题:1.您知道发生此异常的任何原因吗?我不想只是忽略异常。而且,当大多数设备都能正常运行时,我也不想删除此代码。2.您知道一些新的可靠方法,如何在删除某些文件时通知MediaScanner,以及如何立即刷新MTP的内容吗?
我正在尝试将应用程序中的图像保存到手机的默认图库中。如果我的手机上有 SD 卡,下面的代码可以完美运行。正如预期的那样,保存的图像出现在手机的图库和所有内容中:
private Uri saveMediaEntry(File f, String title, String description, int orientation, Location loc) {
ContentValues v = new ContentValues();
v.put(Images.Media.TITLE, title);
v.put(Images.Media.DISPLAY_NAME, title);
v.put(Images.Media.DESCRIPTION, description);
v.put(Images.Media.ORIENTATION, orientation);
String nameFile = f.getName();
File parent = f.getParentFile() ;
String path = parent.toString().toLowerCase() ;
String nameParent = parent.getName().toLowerCase() ;
v.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, nameParent);
v.put(Images.Media.SIZE,f.length()) ;
if( nameFile.toLowerCase().contains(".png") ){
v.put(Images.Media.MIME_TYPE, "image/png");
}else if( nameFile.toLowerCase().contains(".jpg") ||
nameFile.toLowerCase().contains(".jpeg") ){
v.put(Images.Media.MIME_TYPE, "image/jpeg");
}else{
v.put(Images.Media.MIME_TYPE, "image/jpeg");
}
String imagePath = f.getAbsolutePath();
v.put("_data", imagePath) ;
ContentResolver c …Run Code Online (Sandbox Code Playgroud)