2015-03-13 23 views
-1

我試圖開發一個android應用程序,從相機捕獲圖像,然後將其顯示在ImageView上,但它每次在應用程序捕獲圖像後崩潰....謝謝如何獲取位圖圖像內部onActivityResult(){}

這裏是我的javacode

public class MainActivity extends ActionBarActivity { 
private ImageView mImageView; 
    static final int REQUEST_IMAGE_CAPTURE = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mImageView=(ImageView)findViewById(R.id.image); 

     } 
    public void klik(View view){ 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
     } 
    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
      Bundle extras = data.getExtras(); 
      Bitmap imageBitmap = (Bitmap) extras.get("data"); 
      mImageView.setImageBitmap(imageBitmap); 
     } 
    } 
    @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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

回答

0

我這樣做的:

  1. 創建一個臨時文件的圖像和存儲路徑保存在一個成員變量。
  2. 填寫相機意圖的EXTRA_OUTPUT參數。
  3. OnActivityResult從您之前存儲的路徑中讀取文件。

隨着代碼是這樣的:

// 1. 
File tempFile = File.createTempFile("camera", ".png", getExternalCacheDir()); 
mPath = tempFile.getAbsolutePath(); 
Uri uri = Uri.fromFile(tempFile); 

// 2. 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); 

// 3. 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    Bitmap bitmap = BitmapFactory.decodeFile(mPath, options); 

    mImageView.setImageBitmap(bitmap); 
} 
+0

三江源非常..........工作 – 2015-03-13 17:10:40