Android 将文件保存到内部存储器并在库中显示
我应该在内存中保存一个图像(字节[]),并使其在库中可见,但是下面的代码不起作用,怎么了?文件不会保存并显示在库中,但不会生成错误Android 将文件保存到内部存储器并在库中显示,android,file,gallery,Android,File,Gallery,我应该在内存中保存一个图像(字节[]),并使其在库中可见,但是下面的代码不起作用,怎么了?文件不会保存并显示在库中,但不会生成错误 private void saveImageToDisk(final byte[] bytes) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hh:mm"); String todayDate = dateFormat.format(new Date()); L
private void saveImageToDisk(final byte[] bytes) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hh:mm");
String todayDate = dateFormat.format(new Date());
Log.d("Foto data: ",todayDate);
File file = new File(context.getFilesDir(), "image.jpg");
try (final OutputStream output = new FileOutputStream(file)) {
output.write(bytes);
output.close();
} catch (final IOException e) {
Log.e("TAG", "Exception occurred while saving picture to storage ", e);
}
Log.d("Foto","File salvato"+ file.getAbsolutePath());
addPicToGallery(file.getPath());
}
private void addPicToGallery(String photoPath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file = new File(photoPath);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
将文件保存到内部存储器并在库中显示
那是不可能的,对不起
但是下面的代码不起作用,怎么了
第三方应用程序(包括
MediaStore
和图库)无权访问您的文件。要将图像保存到内部存储器,您可以使用以下功能:
String saveImage(Context context, Bitmap image) {
String savedImagePath = null;
// Create the new file in the external storage
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + ".jpg";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/YourAppName");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
// Save the new Bitmap
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(context, savedImagePath);
// Show a Toast with the save location
String savedMessage = context.getString(R.string.saved_message, savedImagePath);
Toast.makeText(context, savedMessage, Toast.LENGTH_SHORT).show();
}
return savedImagePath;
}
此函数将上下文和位图作为参数,并返回存储的文件路径
要使图像对默认库可见,请执行以下操作:
static void galleryAddPic(Context context, String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
您需要将存储的图像的上下文和路径传递给此函数。第二个函数在第一个函数内调用
我不确定您是否需要为这些目的创建一个文件提供程序,但如果需要的话
在
标记内的AndroidManifest.xml
中,使用以下命令:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="<YOUR.PACKAGE.NAME>.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
问题是什么?文件未保存?是否有堆栈跟踪?文件未保存并显示在库中,但未生成错误Hank's,如何解决?我要把图像保存在哪里?@Daniele:你会想把它保存在电脑上的某个地方。如果在用户安装应用程序后图像应该保留,请根据
环境选择一个位置。getExternalStoragePublicDirectory()
。
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path name="my_cache" path="." />
<external-path name="my_images" path="Pictures/" />
</paths>