根據測試,書中的代碼,在自己的5.1 版本的華為手機上是不能正常跑的。調用的圖片並不能正確顯示,在公司的4.x版本的手機上顯示正常。
代碼如下
package org.quentin.choosepictest; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import android.app.Activity; 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.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; /* * 經過測試,發現這些代碼在公司的測試機上跑起來是沒有問題的。但是在自己的5.1手機測試結果是不正常的。 * 這可真是奇怪啊。學習一本書的時候,還是有一個比較穩定的版本比較好。當入門以后在通過自己查找文檔,獲得新特性 * 在真正的項目開發中才可能事半功倍 */ public class MainActivity extends Activity { public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; private Button takePhoto; private ImageView picture; private Uri imageUri; // - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takePhoto = (Button)findViewById(R.id.take_photo); picture = (ImageView)findViewById(R.id.picture); // takePhoto.setText("Hello Thoto"); takePhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //TODO Auto-generated method stub File outputImage = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg"); try{ if(outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); }catch(IOException e){ e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_PHOTO); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ case TAKE_PHOTO: if(resultCode == RESULT_OK){ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("scale", 1); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO); } break; case CROP_PHOTO: if(resultCode == RESULT_OK){ try{ Bitmap bitmap = BitmapFactory.decodeStream( getContentResolver().openInputStream(imageUri) ); picture.setImageBitmap(bitmap); }catch(FileNotFoundException e){ e.printStackTrace(); } } break; default: break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
原因暫時還不太明白,用 Log 打印和 各種條件調試,也不正常。目前在熟悉代碼階段,暫時不深究了,標記一下,以后在深入研究一下吧。