我是Android的新手,并试图使用现有的相机制作应用程序。 我试图将图像保存在dcim目录中。 现在应用程序创建一个文件,但没有图像,我怎么能前进,如何保存图像 提前谢谢
======================== CAMERA ===============================
public static class App {
public static File file;
public static File directory;
}
private void CreateDirectoryForPictures() {
File path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
App.directory = new File(path,"Mahapach_Images");
if (!App.directory.exists()) {
App.directory.getParentFile().mkdirs();
}
}
private void takeAPicture(){
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
CreateDirectoryForPictures();
Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
App.file = new File(App.directory,imageFileName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(App.file));
startActivityForResult(intent,1);
}
private void choosePhotoFromGallery() {
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
//make it available in the gallery
Intent mediaScanIntent = new Intent (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(App.file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.e("path image from gallery", picturePath + "");
infoWindowImageView.setImageBitmap(thumbnail);
}
}
}
答案 0 :(得分:0)
您可以通过谷歌轻松找到许多教程,这可以完成您的任务,我发现here
OR
有更好的方法,使用Easy Image库可以轻松完成任务click here但请记住你必须照顾marshmallow和Android版本以上的权限。