标签:
根据测试,书中的代码,在自己的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 打印和 各种条件调试,也不正常。目前在熟悉代码阶段,暂时不深究了,标记一下,以后在深入研究一下吧。
标签:
原文地址:http://www.cnblogs.com/lbfamous/p/5075480.html