我已经浏览了几个文档和堆栈,但是我不太确定如何实现这个...
帮助或示例代码可以帮助我了解更多信息。
以下是运行相机的代码组,它的工作非常好,我的下一个问题是,如何让它自动保存到手机图库中?
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tvCameraTiles = (TextView) findViewById(R.id.tvCameraTiles);
tvCameraTiles.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dispatchTakePictureIntent();
}
});
}
private void dispatchTakePictureIntent()
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
//Get Camera Image File Data
// Bundle extras = data.getExtras();
// Bitmap imageBitmap = (Bitmap) extras.get("data");
// mImageView.setImageBitmap(imageBitmap);
}
}
答案 0 :(得分:3)
你读过这个https://developer.android.com/training/camera/photobasics.html吗?
特别是部分:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
您似乎无法将照片保存在外部存储设备中,因此它应该可以正常工作。
编辑:我尝试使用方法galleryAddPic的文档制作一个非常基本的应用程序。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button photoButton = (Button) findViewById(R.id.photobutton);
photoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dispatchTakePictureIntent();
}
});
}
static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//Bundle extras = data.getExtras();
//Bitmap imageBitmap = (Bitmap) extras.get("data");
//mImageView.setImageBitmap(imageBitmap);
galleryAddPic();
}
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
File storageDir = Environment.getExternalStorageDirectory();
File image = File.createTempFile(
"example", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
这对我有用!我使用相机应用程序拍照,然后它在画廊应用程序中显示。 我修改了关于FileProvider部分的代码,现在它在方法Uri.fromFile(photoFile)的帮助下传递了Uri。我也将照片保存在不同的位置,检查createImageFile方法中的代码。
在Android Studio模拟器工作正常,请告诉我你的事。
答案 1 :(得分:0)
首先创建文件,然后将ByteStream写入文件并保存。您不会对此文件执行任何操作以显示在库中。这将由图库自动刷新。
答案 2 :(得分:0)
我试用了Rex B代码段,但最终在Android Studio上收到错误消息。
android.os.FileUriExposedException: file:///storage/emulated/0/example6941495009290613124.jpg exposed beyond app through ClipData.Item.getUri()
如果有人遇到此错误,我发现了一个简单的Keras.Sequential()
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
我将这两行添加到onClick方法
Button photoButton = (Button) findViewById(R.id.photobutton);
photoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
dispatchTakePictureIntent();
}
});
}
我知道这是旧帖子,但是我相信有人会在这一天到来。