我能';在Android中,是否无法将我的位图保存到库中?
我正在尝试将编辑后的图像保存到Gallery中,用户可以从中访问它。 我使用了参考资料中的“将照片添加到图库”: 这是我的密码:我能';在Android中,是否无法将我的位图保存到库中?,android,file,bitmap,android-gallery,android-mediascanner,Android,File,Bitmap,Android Gallery,Android Mediascanner,我正在尝试将编辑后的图像保存到Gallery中,用户可以从中访问它。 我使用了参考资料中的“将照片添加到图库”: 这是我的密码: String mCurrentPhotoPath; public void startSave() { FileOutputStream fileOutputStream = null; File new_file = null; try { new_file = createImageFile(); fil
String mCurrentPhotoPath;
public void startSave() {
FileOutputStream fileOutputStream = null;
File new_file = null;
try {
new_file = createImageFile();
fileOutputStream = new FileOutputStream(new_file);
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
Toast.makeText(this, "save Image Success", Toast.LENGTH_SHORT).show();
fileOutputStream.flush();
fileOutputStream.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
我还像这样更改了AndroidManifest.xml
,并授予了在外部存储中写入的权限和Uri权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mypackage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
只有在媒体扫描程序成功完成后,才会显示该文件
注
将文件保存到外部存储器。现在路径是Android数据文件夹。大多数情况下,介质扫描仪不会扫描数据文件夹中的文件。
为此,请使用Environment.getExternalStorageDirectory().getAbsolutePath()+“/Pictures/Foldername/”
作为文件夹路径
Environment.getExternalStorageDirectory().getAbsolutePath()
将外部存储作为路径返回
另请参阅是否检查您是否具有写入或读取权限?您必须在输出文件夹上运行Media Scanner。检查我的回答我已使用以下代码检查权限:if(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.permission_DENIED){Toast.makeText(this,“permission DENIED”,Toast.LENGTH_LONG.show();}。。。。。。。我认为许可被拒绝了。我已经在android清单中添加了它,所以我不知道@joe出了什么问题。你发布问题的方式非常奇怪。您正在尝试保存位图<代码>文件storageDir=getExternalFilesDir(Environment.DIRECTORY\u图片)代码>。正如您所看到的,您正试图将其保存到外部存储。现在,首先告诉我们该文件是否已创建,或者是否存在io异常。如果文件已创建,您可以使用设备上的任何文件资源管理器应用程序进行检查。请通知我们。创建文件后,您可以尝试在mediastore中获取它。如果是在媒体strore中,则Gallery app等应用程序将显示您的文件。代码的错误在于,即使存在FileNotFoundException或IOException,您仍然会调用扫描仪。你当然不应该那样做。您应该在这些catch块中显示一个toast来通知用户,然后返回。请调整您的代码。我正在使用内部存储器保存图像并将其添加到多媒体资料中,因为并非所有手机都有外部存储@tomin B这将返回sdcard0路径,该路径为内部存储器。但它被称为外部存储器
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="Android/data/com.sparkexel.blurd1/files/Pictures" />
</paths>