我应该在内部存储器中保存一个图像(byte [])并使其在库中可见,但是下面的代码不起作用,这有什么问题?文件未保存并显示在库中,但未生成错误
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);
}
答案 0 :(得分:1)
要将图像保存到内部存储,您可以使用以下功能:
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;
}
此函数将上下文和Bitmap
图像作为参数,并返回存储文件的路径。
并使图像对默认图库可见:
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);
}
您需要将存储的图像的上下文和路径传递给此函数。第二个函数在第一个函数内部调用。
我不确定你是否需要为这些目的实际制作FileProvider,但如果你这样做的话。
在AndroidManifest.xml
标记内<application>
,请使用以下内容:
<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>
并创建一个新的Resource目录。右键单击“res”&gt;&gt; New Android Resource Directory
。从下拉列表中选择“xml”。然后右键单击xml文件夹并创建一个名为file_paths
的文件,其中put:
<?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>
答案 1 :(得分:0)