这就是事情。 我正在构建一个应用程序,每次用户检查一个复选框,它打开相机应用程序,以拍摄与特定复选框内容相关的图片。拍摄照片并将其保存在app文件夹中的过程已经有效。现在我只需要知道如何将这些拍摄的照片添加到图库中。下面是我的相机类。
public class anexarFotos extends AppCompatActivity {
SharedPreferences dadospasta;
String projectName;
File directory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dadospasta = getSharedPreferences(PREFERENCES, MODE_PRIVATE);
projectName = dadospasta.getString("folderDir",null);
directory = new File(Environment.getExternalStorageDirectory() + projectName);
dispatchTakePictureIntent();
finish();
}
String mCurrentPhotoPath;
static final int REQUEST_TAKE_PHOTO = 1;
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView mImageView;
public void obraFolder(){ //Criar a pasta do projeto e o diretório em que os projetos estarão conditos, caso não tenha sido criado.
if (!directory.exists()){
directory.mkdirs();
}
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {photoFile = createImageFile();
} catch (IOException ex) {
Context context = getApplicationContext();
CharSequence text = "Foto não pode ser armazenada.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Context context = getApplicationContext();
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.relatoriodeobras.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}else{
Context context = getApplicationContext();
CharSequence text = "Atenção! Obrigatório tirar foto!!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
finish();
}
}
private File createImageFile() throws IOException {
obraFolder();
// Create an image file name
Context context = getApplicationContext();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = projectName + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
addImageToGallery(imageFileName, context);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
public static void addImageToGallery(final String projectName, final Context context) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, projectName);
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
}
第一部分displayTakePictureIntent();来自原始android开发者的页面。我在这里发现的addImageToGallery方法。你能帮我吗?在我寻找答案时,我看到有人使用ContentValues将其添加到图库中。以下是我发现的内容。
public Uri addImageToGallery(ContentResolver cr, String imgType, File filepath) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "player");
values.put(MediaStore.Images.Media.DISPLAY_NAME, "player");
values.put(MediaStore.Images.Media.DESCRIPTION, "");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/" + imgType);
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.DATA, filepath.toString());
return cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
我是否需要一个ContentValues才能做到这一点,或者我的addImageToGallery应该可以做到这一点?
非常感谢提前!