Android 捕获图像并将其保存在内部存储器中
我正在尝试拍摄一张照片,将其保存在内存中,然后在imageview中显示(因为我无法访问内存并查找图像) 拍摄照片似乎可行,但将图像加载到带有uri的imageview不起作用 (intent返回的uri是:“file:/data/user/0/com.packagename/filesfoldername/filename”)。imageview保持为空Android 捕获图像并将其保存在内部存储器中,android,image,camera,internal-storage,Android,Image,Camera,Internal Storage,我正在尝试拍摄一张照片,将其保存在内存中,然后在imageview中显示(因为我无法访问内存并查找图像) 拍摄照片似乎可行,但将图像加载到带有uri的imageview不起作用 (intent返回的uri是:“file:/data/user/0/com.packagename/filesfoldername/filename”)。imageview保持为空 private static String date = new SimpleDateFormat("ddMMyyy").format(ne
private static String date = new SimpleDateFormat("ddMMyyy").format(new Date());
private File sessionDirectory=null;
private ImageView imgView;
private Uri HelpUri;
@override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case REQUEST_IMAGE_CAPTURE:
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK) {
Toast.makeText(this, "Image Saved!", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Uri= " + HelpUri, Toast.LENGTH_LONG).show();
}
else
Toast.makeText(this, "Error Taking Photo!", Toast.LENGTH_SHORT).show();
break;
}
}
//Method creates an Intent to the camera - Capture an Image and save it//
private void openCamera(String Pose) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File ImageFile = null;
try {
ImageFile = createImageFile(Pose);
} catch (IOException ex) {
//Something for errors..
}
if (ImageFile != null) {
Uri ImageURI = android.net.Uri.parse(ImageFile.toURI().toString());
HelpUri = ImageURI;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, ImageURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
else
Toast.makeText(this, "Problem Accessing Internal Storage", Toast.LENGTH_SHORT).show();
}
}
//Methods returns a File for the image file created on the internal storage//
private File createImageFile(String Pose) throws IOException {
if(sessionDirectory==null)
createSessionFolder();
if(sessionDirectory!=null) { //Succeed creating/finding the session directory
return File.createTempFile(
Pose, /* prefix */
".jpg", /* suffix */
sessionDirectory /* directory */
);
}
else
return null;
}
//Method creates the session directory - update the field if existed, creates it if not//
private void createSessionFolder() {
sessionDirectory = new File(getFilesDir()+"Session_"+date);
if (!sessionDirectory.exists())
if(!sessionDirectory.mkdirs()) //Tried to create the directory buy failed
sessionDirectory = null;
}
如果有人能帮助我,我将不胜感激
多谢各位
private static final int REQUEST_IMAGE_CAPTURE = 1;
private String picture_directory = "/picturedir/";
public void openCamera(Context context) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null){
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri urifromFile = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
urifromFile);
context.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
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 = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES+picture_directory);
storageDir.mkdirs();
image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
将提供程序路径添加到res/xml文件夹。并在AndroidManifest.xml上声明它。更多信息请点击这里
第三方应用无法写入应用的内部存储部分。
该意图返回的uri是:“file:/data/user/0/com.packagename/filesfoldername/filename”
不可能。这将是“file:///data/user/0/com.packagename/filesfoldername/filename“相反。你完全正确,这是我的错误。这是进程创建的uri,用于检测目录和文件名…内部存储不需要权限。如果使用外部存储,则不需要文件提供程序。不低于7.0。@iravul为什么使用Environment.getExternal。。。?我想将图像保存在内部存储器上。什么应该取代它?
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.package"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
public void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}