Android Studio将图像保存到新目录中
我的应用程序有3个按钮:识别人脸、多媒体资料和分析。“识别”按钮的作用是拍照。如何将拍摄的照片保存到我的多媒体资料目录中Android Studio将图像保存到新目录中,android,android-button,android-gallery,Android,Android Button,Android Gallery,我的应用程序有3个按钮:识别人脸、多媒体资料和分析。“识别”按钮的作用是拍照。如何将拍摄的照片保存到我的多媒体资料目录中 public class MainMenu extends AppCompatActivity { Button btnTakePhoto; ImageView imgTakenPhoto; private static final int CAM_REQUEST = 1313; private static Button button_s
public class MainMenu extends AppCompatActivity {
Button btnTakePhoto;
ImageView imgTakenPhoto;
private static final int CAM_REQUEST = 1313;
private static Button button_sbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
OnClickButtonListener();
btnTakePhoto = (Button) findViewById(R.id.button_recognizeface);
imgTakenPhoto = (ImageView) findViewById(R.id.imageview1);
btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAM_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imgTakenPhoto.setImageBitmap(thumbnail);
}
}
public void OnClickButtonListener() {
button_sbm = (Button)findViewById(R.id.button_gallery);
button_sbm.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View V) {
Intent intent = new Intent("com.example.syafiq.facerecognition.Gallery");
startActivity(intent);
}
}
);
}
class btnTakePhotoClicker implements Button.OnClickListener
{
@Override
public void onClick(View v) {
Intent cameraintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraintent, CAM_REQUEST);
}
}
通过下面的代码,它的工作对我来说很好
private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {
File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");
if (!direct.exists()) {
File wallpaperDirectory = new File("/sdcard/DirName/");
wallpaperDirectory.mkdirs();
}
File file = new File(new File("/sdcard/DirName/"), fileName);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}您的问题以前已经得到了回答,只需搜索如何将位图转换为文件。非常感谢suzan:D但当我试图保存图片时,我的应用程序不幸停止工作。:/很抱歉,我真的不擅长android编程/检查logcat上是否有任何错误。如果你在那里发现了什么,你可以再问一次问题:)谢谢你,苏珊:D就可以了;)11-22 17:01:41.051 16572-16572/com.example.syafiq.facialrecognition E/﹕ 设备驱动程序API匹配设备驱动程序API版本:29用户空间API版本:29 11-22 17:01:41.051 16572-16572/com.example.syafiq.facialrecognition E/﹕ 马里:版本=Linux-r3p2-01rel3构建日期=2014年7月22日星期二19:59:34