我正在使用一个按钮上的一键来捕捉图像。

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代码中拍摄的图像,但可以通过文件浏览器查看图像,当我在PC中打开SD卡时,我可以看到图像,然后Android在Gallery中显示图像,代码中也有。
让我知道问题是什么以及如何解决,期待您的答复。
谢谢。

最佳答案

android扫描媒体文件和那些在使用content provider时显示的文件…当您运行应用程序并拍摄新相机时,您刚刚拍摄了新照片,但默认库不知道这些新照片,因为它们不在内容提供商的媒体列表中。
我认为在拍照后和打电话给Gallery之前添加这条线将显示从相机拍摄的所有最新照片。

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

这将在kitkat+中引发securityexception。请尝试Vossi的方法,或使用此处使用的MediaScannerConnection:stackoverflow–@absk

关于android - 拍照后更新图库图库未显示所有图片Android相机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8667623/

10-11 22:40
查看更多