我将图像添加到SDCARD上的文件夹中.由于图像和我的文件夹在图库中没有立即可见,我试图让MediaScannerConnection更新并在图库中显示文件夹/图像.这对我来说不是很好,因为Gallery中没有任何内容.我只在Eclipse AVD中测试.
我没有看到很多关于这个的讨论,因为scanFile是自api8以来的新功能.有人能说明这是怎么做的吗?
我在服务和Activity中尝试它但在onScanCompleted时继续获得uri = null.
我在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();
}};
Run Code Online (Sandbox Code Playgroud)
图像保存在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);
} …Run Code Online (Sandbox Code Playgroud)