拍照后更新图库图库未显示所有图片Android相机

时间:2011-12-29 12:01:58

标签: android android-camera

我在按钮上使用onClick来捕捉图像。

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卡中。 然后用户单击“发送”按钮并打开布局,在“单击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);
}

但我无法查看在图库代码中捕捉的图像,但可以通过FileBrowser查看图像,当我在PC中打开SD卡时,我可以看到图像,然后Android在图库中显示图像。码。 让我知道问题是什么以及如何解决,期待您的回复。 感谢。

3 个答案:

答案 0 :(得分:7)

Android扫描媒体文件和那些在使用content provider时显示...当你运行你的应用程序并拍摄新相机时你只是拍了新照片但是默认画廊不知道这个新的照片原因它们不在内容提供商的媒体列表中。

我认为在拍照后和调用图库之前添加此行将显示从相机拍摄的所有最新照片。

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                 Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

这将在KitKat +中抛出SecurityException。尝试使用Vossi的方法,或使用此处使用的MediaScannerConnection:stackoverflow - @absk

答案 1 :(得分:7)

即使这有点老了!但我遇到了同样的问题。 Soni的回答很有效,但每次都要求Media Scanner在整个目录中查找新文件。您也可以注册一个看起来更高效的新文件

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+f.getAbsolutePath())));

f是您要注册的文件。

答案 2 :(得分:-1)

  1. 简单的答案是首先运行应用程序,从应用程序的摄像头捕获图像,然后在设备的图库中查看。

  2. 您无法通过应用程序查看上次捕获的图像,一次重启设备,并且可以看到您的上一个捕获图像位于设备的库中。

  3. 还有一件事,在您的设备重新启动后,再次打开您自己的应用程序并再次从您的应用程序捕获一个图像,并看到您的第二个捕获图像可以在图库中看到而无需在您的设备中执行任何操作。

相关问题