簡體   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