将相机中的图像存储在新文件夹中 android
Storing image from camera in new folder android
我正在创建一个应用程序,我希望用户在其中选择要在其中保存图像的文件夹。我首先想看看是否可以弄清楚如何将图像保存在默认文件夹之外的另一个文件夹中,但我保留了收到错误,无法弄清楚原因。
这是我必须启动相机意图并获取文件目录名称的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart_wardrobe);
camera = (Button)findViewById(R.id.cameraBtn);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
file = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}
});
}
这是 uriFileDirectory 的代码
private static Uri getOutputMediaFileUri(){
return Uri.fromFile(getOutputMediaFile());
}
private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
return mediaFile;
}
这是我的 onActivityResult 方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
Toast.makeText(this, "There is someting wrong with your code", Toast.LENGTH_LONG).show();
}
}
if (requestCode == SELECT_PICTURE){
if (resultCode == RESULT_OK) {
images = data.getData();
selectedImagePath = getPath(images);
}
}
}
有人可以帮助修复我的代码吗?
首先检查您在清单中的许可。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
和 onActivityResult
方法检查它是否返回文件路径。在此方法中添加行
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
file, Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
Toast.makeText(this, "There is someting wrong with your code", Toast.LENGTH_LONG).show();
}
}
问题出在你的吐司上。在 Camera Intent 中,我们将 intent 设置为 null,而您正试图从那里获取数据。你必须使用 Uri。
我正在创建一个应用程序,我希望用户在其中选择要在其中保存图像的文件夹。我首先想看看是否可以弄清楚如何将图像保存在默认文件夹之外的另一个文件夹中,但我保留了收到错误,无法弄清楚原因。
这是我必须启动相机意图并获取文件目录名称的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart_wardrobe);
camera = (Button)findViewById(R.id.cameraBtn);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
file = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE );
}
});
}
这是 uriFileDirectory 的代码
private static Uri getOutputMediaFileUri(){
return Uri.fromFile(getOutputMediaFile());
}
private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
if (! mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
return mediaFile;
}
这是我的 onActivityResult 方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
Toast.makeText(this, "There is someting wrong with your code", Toast.LENGTH_LONG).show();
}
}
if (requestCode == SELECT_PICTURE){
if (resultCode == RESULT_OK) {
images = data.getData();
selectedImagePath = getPath(images);
}
}
}
有人可以帮助修复我的代码吗?
首先检查您在清单中的许可。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
和 onActivityResult
方法检查它是否返回文件路径。在此方法中添加行
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
file, Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
Toast.makeText(this, "There is someting wrong with your code", Toast.LENGTH_LONG).show();
}
}
问题出在你的吐司上。在 Camera Intent 中,我们将 intent 设置为 null,而您正试图从那里获取数据。你必须使用 Uri。