拍照后更新图库图库未显示所有照片Android摄像头
我用鼠标点击按钮来捕捉图像拍照后更新图库图库未显示所有照片Android摄像头,android,android-camera,Android,Android Camera,我用鼠标点击按钮来捕捉图像 PictureCallback myPictureCallback_JPG = new PictureCallback() { @Override public void onPictureTaken(byte[] arg0, Camera arg1) { FileOutputStream outStream = null; try { // Write to SD Card
PictureCallback myPictureCallback_JPG = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
FileOutputStream outStream = null;
try {
// Write to SD Card
outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(arg0);
outStream.close();
Toast.makeText(Photo.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
System.out.println();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}};
图像将保存在SD卡中。
然后,用户单击Send按钮,打开布局,单击ImageView打开图像库,单击特定图像,URI被选中
imageAttachPhoto = (ImageView)findViewById(R.id.imageViewPhotoOne);
imageAttachPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
imageAttachPhoto.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
但我无法查看在Gallery代码中拍摄的图像,但可以通过FileBrowser查看图像,当我在PC中打开SD卡时,我可以看到图像,然后Android在Gallery中显示图像,代码中也包含图像。
让我知道问题是什么以及如何解决,期待您的回复。
谢谢。Android扫描媒体文件,这些文件在使用
内容提供商时显示。
当您运行应用程序并拍摄新相机时,您只拍摄了新照片,但默认库不知道这些新照片,因为它们不在内容提供商的媒体列表中
我想在拍照后和打电话给gallery之前添加这条线将显示从相机拍摄的所有最新照片
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
这将在KitKat+中引发SecurityException。尝试Vossi的方法,或者使用此处使用的MediaScannerConnection:–@absk即使这有点旧!但我也有同样的问题。Soni的答案是可行的,但每次都需要媒体扫描器在整个目录中查找新文件。你也可以注册你的新文件,看起来性能更好
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+f.getAbsolutePath())));
f是要注册的文件
再加上一个绝对完美的…让它工作了…但每当我想在肖像模式下拍照时,方向会旋转90度。任何关于如何纠正的想法。谢谢。嗯……没有完全理解您,因为您正在调用默认意图……无论如何……如果没有通过internet完成解决方案,请在SO上提出另一个问题,人们会帮助您。这将在KitKat+中引发安全异常。请尝试Vossi的方法,或使用此处使用的MediaScannerConnection:@absk感谢您的评论。跟上
java.lang.SecurityException:权限拒绝:不允许发送广播android.intent.action.MEDIA_,从pid=11300,uid=10512装载这太棒了。顺便说一下,您也可以直接使用Uri.fromFile(f),而不是Uri.parse()。看起来好一点,做同样的事情。:)这正是我要找的,比扫描整个目录要快得多。我使用的是fromFile方法,它工作得很好。Uri.fromFile(file)甚至更好