简体   繁体   中英

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

Hai am new in Android Application.i Generated a code to take picture by using Camera.Now i want to view the Taken Pictures without using Database.i dont know how to do this.AnyBody please help me. Thanks in advance Here my Code

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

Simple as this:

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

Default app for viewing pictures will start, or if there's no default, user get selection dialog which app to use.

you really dont need to use database to view taken picture.

  1. To get new image - open an intent for Camera. like this
  2. To see all other captured images, open Gallery. see this discussion

heres a good overview of opening data via intents on your phone. All this documentation is in here as well http://developer.android.com/guide/topics/intents/intents-filters.html but not as clear to follow.

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

Now if what your looking for is to display the picture you took in your own activity you can use.

String file = String.format("/sdcard/%d.jpg", System.currentTimeMillis());
//SAVE IMAGE TO file
img.setImageURI(URI.parse(file));

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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