如何在Android上查看已拍摄的照片而不使用数据库?

5

我是一个新手,正在学习Android应用开发。我写了一段代码来使用相机拍照,现在我想在不使用数据库的情况下查看所拍摄的图片。但我不知道该如何实现。有没有人可以帮帮我? 非常感谢您的帮助。 以下是我的代码:

private void createCamera() {
   // TODO Auto-generated method stub
   preview = new Preview(this);
   ((FrameLayout) findViewById(R.id.preview)).addView(preview);

   buttonClick = (Button) findViewById(R.id.buttonClick);
   buttonClick.setOnClickListener( new OnClickListener() {
      public void onClick(View v) {
         preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
      }
   });

  Log.d(TAG, "onCreate'd");
}

ShutterCallback shutterCallback = new ShutterCallback() {
   public void onShutter() {
      Log.d(TAG, "onShutter'd");
   }
};

/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
   public void onPictureTaken(byte[] data, Camera camera) {
      Log.d(TAG, "onPictureTaken - raw");
   }
};

/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
   public void onPictureTaken(byte[] data, Camera camera) {
      FileOutputStream outStream = null;

      try {
         outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis())); 
         outStream.write(data);
         outStream.close();
         Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } finally {}

      Log.d(TAG, "onPictureTaken - jpeg");
   }
};

private void createpictures() {
   img = (ImageView)findViewById(R.id.ImageView01);

   ((Button) findViewById(R.id.Button01)).setOnClickListener(new OnClickListener() {
      public void onClick(View arg0) {
         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);
         img.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);
}

拍摄的照片将自动保存在相册中。 - Mercy
3个回答

1

简单如此:

Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse("file://"+yourFileName));
startActivity(in);

将启动默认的查看图片应用程序,如果没有默认值,则用户会得到选择对话框以选择使用哪个应用程序。


0

这里有一个很好的概述,介绍如何通过手机意图打开数据。所有这些文档都在这里 http://developer.android.com/guide/topics/intents/intents-filters.html,但不太容易理解。

http://indyvision.net/2010/03/android-using-intents-open-files/

 File imageFile2View = new File("/sdcard/nice_photo.jpeg");
 Intent i = new Intent();
 i.setAction(android.content.Intent.ACTION_VIEW);
 i.setDataAndType(Uri.fromFile(imageFile2View), "image/jpeg");
 startActivity(i);

现在,如果你想在自己的活动中显示所拍摄的照片,你可以使用以下方法。
String file = String.format("/sdcard/%d.jpg", System.currentTimeMillis());
//SAVE IMAGE TO file
img.setImageURI(URI.parse(file));

或者

String file = String.format("/sdcard/%d.jpg", System.currentTimeMillis());
//SAVE IMAGE TO file
bitmap = BitmapFactory.decodeFile(file);
img.setImageBitmap(bitmap);

0

实际上,您不需要使用数据库来查看已拍摄的图片。

  1. 要获取新图像 - 打开相机意图。例如 this
  2. 要查看所有其他捕获的图像,请打开图库。请参见此discussion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接