android如何拍照以及返回拍的圖片(經過驗證的實際例子)

來源:互聯網
上載者:User
package a.test;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import android.app.Activity;import android.content.ActivityNotFoundException;import android.content.ContentResolver;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.Gravity;import android.widget.ImageView;import android.widget.Toast;public class a extends Activity { /** Called when the activity is first created. */ String SD_CARD_TEMP_DIR; private byte[] mContent; Bitmap myBitmap; @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  //  SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory()    + File.separator + "tmpPhoto.jpg";  Intent takePictureFromCameraIntent = new Intent(    MediaStore.ACTION_IMAGE_CAPTURE);  takePictureFromCameraIntent.putExtra(    android.provider.MediaStore.EXTRA_OUTPUT, Uri      .fromFile(new File(SD_CARD_TEMP_DIR)));  startActivityForResult(takePictureFromCameraIntent, 10);  // } // @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {  ContentResolver resolver = getContentResolver();  try {   File f = new File(SD_CARD_TEMP_DIR);   try {    Uri capturedImage = Uri      .parse(android.provider.MediaStore.Images.Media        .insertImage(getContentResolver(), f          .getAbsolutePath(), null, null));    // Log.i("camera", "Selected image: " +    // capturedImage.toString());    // f.delete();    Toast toast = Toast.makeText(getApplicationContext(),      capturedImage.toString(), Toast.LENGTH_SHORT);    toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);    toast.show();    //    ImageView a = (ImageView) findViewById(R.id.imageView1);    // 將圖片內容解析成位元組數組    mContent = readStream(resolver.openInputStream(Uri      .parse(capturedImage.toString())));    // 將位元組數群組轉換為ImageView可調用的Bitmap對象    myBitmap = getPicFromBytes(mContent, null);    // //把得到的圖片綁定在控制項上顯示    a.setImageBitmap(myBitmap);    //    //   } catch (FileNotFoundException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }  } catch (Exception e) {   // TODO: handle exception   System.out.println(e.getMessage());  } } // public static Bitmap getPicFromBytes(byte[] bytes,   BitmapFactory.Options opts) {  if (bytes != null)   if (opts != null)    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,      opts);   else    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);  return null; } // public static byte[] readStream(InputStream inStream) throws Exception {  byte[] buffer = new byte[1024];  int len = -1;  ByteArrayOutputStream outStream = new ByteArrayOutputStream();  while ((len = inStream.read(buffer)) != -1) {   outStream.write(buffer, 0, len);  }  byte[] data = outStream.toByteArray();  outStream.close();  inStream.close();  return data; } // public static Bitmap getLoacalBitmap(String url) {  try {   FileInputStream fis = new FileInputStream(url);   return BitmapFactory.decodeStream(fis);  } catch (FileNotFoundException e) {   e.printStackTrace();   return null;  } } //}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.