Muk*_*nda 2 android android-camera
我在onClick上使用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);
}
但是我无法查看在Gallery代码中捕捉的图像,但可以通过FileBrowser查看图像,当我在PC中打开SD卡时,我可以看到图像,然后Android也会在代码中显示图库中的图像.让我知道问题是什么以及如何解决,期待您的回复.谢谢.
Android扫描媒体文件和使用content provider时显示...当您运行您的应用程序并采取新相机时,您刚刚拍摄新照片但默认图库不知道新的照片因为他们不在内容提供商的列表中媒体.
我想在拍照之后和调用画廊之前添加这一行将显示从相机拍摄的所有最新照片.
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                 Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
这将在KitKat +中抛出SecurityException.尝试使用Vossi的方法,或使用此处使用的MediaScannerConnection:stackoverflow - @absk
小智 7
即使这有点老了!但我遇到了同样的问题.Soni的回答很有效,但每次都要求Media Scanner在整个目录中查找新文件.您也可以注册一个看起来更高效的新文件
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+f.getAbsolutePath())));
f是您要注册的文件.
| 归档时间: | 
 | 
| 查看次数: | 5682 次 | 
| 最近记录: |