2013-03-11 83 views
2

正在關注the android tutorial,我可以拍照。在Android應用程序中顯示拍攝的圖片

public class TakePhotoActivity extends Activity { 

    private Uri fileUri; 
    // I am not sure what this is for yet 
    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_take_photo); 
     // Show the Up button in the action bar. 
     setupActionBar(); 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     fileUri = getOutputMediaFileUri(); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

     startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

    } 

    private static Uri getOutputMediaFileUri() { 
     return Uri.fromFile(getOutputMediaFile()); 
    } 

    private static File getOutputMediaFile() { 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       "MyCameraApp"); 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d("MyCameraApp", "failed to create directory"); 
       return null; 
      } 
     } 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss") 
       .format(new Date()); 
     return new File(mediaStorageDir.getPath() + File.separator + "IMG_" 
       + timeStamp + ".jpg"); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE == requestCode) { 
      if (RESULT_OK == resultCode) { 
//    Toast.makeText(this, "Image saved to:\n" + data.getData(), 
//    Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
} 

現在我想要顯示我剛剛拍攝的圖片,使用onActivityResult。有沒有人有一些代碼來做到這一點?

編輯顯示XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".TakePhotoActivity" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="@string/desc" /> 

</RelativeLayout> 

回答

1

使它像這樣:

public class TakePhotoActivity extends Activity { 

private Uri fileUri; 
// I am not sure what this is for yet 
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; 
ImageView image; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_take_photo); 
    // Show the Up button in the action bar. 
    setupActionBar(); 
    image = (ImageView)findViewById(R.id.imageView1); 

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    fileUri = getOutputMediaFileUri(); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

} 

,然後在這裏把位圖在imageview的這樣

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 100) { 
      try { 
       bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
image.setImageBitmap(bitmap); 


       sendBroadcast(new Intent(
         Intent.ACTION_MEDIA_MOUNTED, 
         Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 





     } 
+0

我是初學者。我正在尋找我能理解的東西。我希望得到的代碼少得多。 – learner 2013-03-12 00:14:01

+0

現在是怎麼回事,你會得到一個非常高的分辨率的位圖和sendBroadcast將更新您的畫廊與你拍的照片,然後在那裏做任何你想要的位圖 – JRowan 2013-03-12 00:27:51

+0

如果你不發送像那樣的廣播圖片不會出現在您的手機中,直到您下次打開手機 – JRowan 2013-03-12 00:32:11

0
Bitmap b = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri) 
myview.setBackground(new BitmapDrawable(b)); 

「MyView的」 從你的任何佈局視圖對象。因此,如果您的佈局包含ID爲「@ + id/myview」的RelativeLayout對象,則第二行可能會說:

findViewById(R.id.myview).setBackground(new BitmapDrawable(b));

+0

我已經編輯我的職務,以顯示我有什麼至今。你能爲我提供實現的'onActivityResult'方法嗎?同時,我將尋找如何創建您提到的myview。這是我第二天學習android。 – learner 2013-03-12 00:16:47

相關問題