拍照并将其保存在内部存储器中
Take picture and save it on internal storage
我正在尝试拍照并将其保存在内部存储器中,这样一来,只有我的应用程序才能访问这些照片
我怎样才能做到这一点?我尝试了很多东西都没有成功,甚至 google 的官方文档也被破坏了,这是我的实际代码,保存在 public 目录中:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pictures);
imageView = (ImageView)findViewById(R.id.imageView);
fabNewPicture = (FloatingActionButton)findViewById(R.id.fab_new_picture);
fabNewPicture.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
takePicture(v);
}
});
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
}
private void takePicture(View v)
{
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = Uri.fromFile(getOutputMediaFile());
i.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(i, 100);
}
private static File getOutputMediaFile()
{
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "FotoAula");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
imageView.setImageURI(file);
}
}
}
但这样图片会变成 public 甚至在图库中也可用,我不希望那样,因为我需要为每张图片添加一些数据库信息,所以我需要它们是私有的到我的应用程序...
我该怎么做?
谢谢!
私人内部存储不exist.When保存图片,尝试删除suffix.Then它不是其他应用程序的图片。
如果您将照片保存在 SD 卡或外部存储器上,任何应用程序都可以访问它。尝试获取私人目录并将您的照片保存在那里。
取自here
创建私有文件并将其写入内部存储:
1-用文件名和操作模式调用openFileOutput()
。这个returns一个FileOutputStream.
2-用write()
写入文件。
3-使用 close()
关闭流。
例如:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
阅读 "Using the Internal Storage" 于:Link
您可以在保存图片的文件夹中创建一个 .nomedia 文件,然后其他应用程序中的媒体扫描仪将忽略您的文件夹
您无法使用相机意图拍照并保存到内部存储器。根据规定,这是不允许的
Android Documentation
Generally, any photos that the user captures with the device camera should be
saved on the device in the public external storage so they are accessible by all
apps.
然而,
However, if you'd like the photos to remain private to your app only, you can instead use the directory provided by getExternalFilesDir(). On Android 4.3 and lower, writing to this directory also requires the WRITE_EXTERNAL_STORAGE permission. Beginning with Android 4.4, the permission is no longer required because the directory is not accessible by other apps, so you can declare the permission should be requested only on the lower versions of Android by adding the maxSdkVersion attribute:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
...
</manifest>
因此您需要将 getOutputMediaFile()
方法更改为:
private static File getOutputMediaFile()
{
File mediaStorageDir = context.getExternalFilesDir("FotoAula");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
我正在尝试拍照并将其保存在内部存储器中,这样一来,只有我的应用程序才能访问这些照片 我怎样才能做到这一点?我尝试了很多东西都没有成功,甚至 google 的官方文档也被破坏了,这是我的实际代码,保存在 public 目录中:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pictures);
imageView = (ImageView)findViewById(R.id.imageView);
fabNewPicture = (FloatingActionButton)findViewById(R.id.fab_new_picture);
fabNewPicture.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
takePicture(v);
}
});
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
}
private void takePicture(View v)
{
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = Uri.fromFile(getOutputMediaFile());
i.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(i, 100);
}
private static File getOutputMediaFile()
{
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "FotoAula");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
imageView.setImageURI(file);
}
}
}
但这样图片会变成 public 甚至在图库中也可用,我不希望那样,因为我需要为每张图片添加一些数据库信息,所以我需要它们是私有的到我的应用程序...
我该怎么做?
谢谢!
私人内部存储不exist.When保存图片,尝试删除suffix.Then它不是其他应用程序的图片。
如果您将照片保存在 SD 卡或外部存储器上,任何应用程序都可以访问它。尝试获取私人目录并将您的照片保存在那里。
取自here
创建私有文件并将其写入内部存储:
1-用文件名和操作模式调用openFileOutput()
。这个returns一个FileOutputStream.
2-用write()
写入文件。
3-使用 close()
关闭流。
例如:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
阅读 "Using the Internal Storage" 于:Link
您可以在保存图片的文件夹中创建一个 .nomedia 文件,然后其他应用程序中的媒体扫描仪将忽略您的文件夹
您无法使用相机意图拍照并保存到内部存储器。根据规定,这是不允许的 Android Documentation
Generally, any photos that the user captures with the device camera should be saved on the device in the public external storage so they are accessible by all apps.
然而,
However, if you'd like the photos to remain private to your app only, you can instead use the directory provided by getExternalFilesDir(). On Android 4.3 and lower, writing to this directory also requires the WRITE_EXTERNAL_STORAGE permission. Beginning with Android 4.4, the permission is no longer required because the directory is not accessible by other apps, so you can declare the permission should be requested only on the lower versions of Android by adding the maxSdkVersion attribute:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
...
</manifest>
因此您需要将 getOutputMediaFile()
方法更改为:
private static File getOutputMediaFile()
{
File mediaStorageDir = context.getExternalFilesDir("FotoAula");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}