繁体   English   中英

如何在不使用数据库的情况下在Android中查看拍摄的图片?

[英]How to view the taken pictures in Android Without using Database?

Hai是Android Application.i中的新手。使用Camera.Now生成一个代码来拍照。我想在不使用Database.i的情况下查看拍摄图片。我不知道怎么做.AnyBody请帮帮我。 在此先感谢我的代码

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);
}

很简单:

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

将开始查看图片的默认应用程序,或者如果没有默认值,则用户获取选择对话框以使用哪个应用程序。

你真的不需要使用数据库来查看拍摄的照片。

  1. 获取新图像 - 打开相机的意图。 这样
  2. 要查看所有其他捕获的图像,请打开图库。 看到这个讨论

这是一个很好的概述,通过手机上的意图打开数据。 所有这些文档都在这里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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM