Android Can';在画廊里看不到最新拍的照片吗?
我正在创建一个android应用程序,在这个应用程序中,我使用相机拍摄照片,然后将照片存储在图片目录中。然后我的应用程序需要上传它。但是,当我从应用程序中拍摄第一张照片时,我在图库中看不到它。但是,从我拍的第二张照片中,我可以在画廊里看到他们。怎么办 这是我的代码:我在图片按钮上显示我的图片:Android Can';在画廊里看不到最新拍的照片吗?,android,android-camera,android-gallery,Android,Android Camera,Android Gallery,我正在创建一个android应用程序,在这个应用程序中,我使用相机拍摄照片,然后将照片存储在图片目录中。然后我的应用程序需要上传它。但是,当我从应用程序中拍摄第一张照片时,我在图库中看不到它。但是,从我拍的第二张照片中,我可以在画廊里看到他们。怎么办 这是我的代码:我在图片按钮上显示我的图片: mImageSelect.setOnClickListener(new View.OnClickListener() { @Override public void on
mImageSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent galleryIntent=new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,GALLERY_REQUEST);
}catch (Exception e){
Toast.makeText(getApplicationContext(),"There was an ERROR: "+e,Toast.LENGTH_LONG).show();
Intent intent=new Intent(PostActivity.this,AllPosts.class);
}
}
});
//我正在用mediastore保存它
MediaScannerConnection.scanFile(HomeActivity.this,
new String[] {imageFile.getAbsolutePath()},
new String[] {"image/jpeg"},null);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageFile)));
对于低于19的API级别,请使用以下方法扫描媒体文件
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
对于API级别19或更高级别的扫描媒体文件,如下所示
private void scanFile(String path) {
MediaScannerConnection.scanFile(MainActivity.this,
new String[] { path }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("TAG", "Finished scanning " + path);
}
});
}
没有拍摄或保存图片的代码。我们也看不到您何时以及如何调用mediascanner。此外,您没有告诉我为什么要自己保存一张照片,而相机应用程序会很乐意为您保存照片。
这里,我将我的照片显示在图像按钮上:
。不,一点也不。在这里,您可以启动一个文件选择器来获取内容。这就是全部。但是,在按钮上显示图像与拍照和上传照片有什么关系呢?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
else
{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}