2013-02-28 115 views
4

請耐心等待...我一直在尋找周圍的爲一個工作,裸骨頭代碼啓動相機活動,拍攝一張照片,並將其放在一個簡單的ImageView>上。 <下面貼出的代碼激活了活動並拍攝了圖片,但圖片沒有顯示在ImageView上!缺少什麼? :'(Android - ImageView不會顯示用相機拍攝的照片

public class MainActivity extends Activity 
{ 
private static final int PICK_IMAGE = 0; 

private static final int PICK_IMAGE_FROM_GALLERY = 1; 

private Button mBtnCamera, mBtnGallery, mBtnCancel; 

private ImageView mImageView; 

private Uri mURI; 
private String mPhotoPath; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mImageView = (ImageView) findViewById(R.id.imgDisplayImage); 

    mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera); 
    mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery); 
    mBtnCancel = (Button) findViewById(R.id.btnCancel); 

    mBtnCamera.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      Intent camera = new Intent(); 

      camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE); 

      camera.putExtra("crop", "true"); 

      File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 

      mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg")); 

      camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI); 

      startActivityForResult(camera, PICK_IMAGE); 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (requestCode == PICK_IMAGE) 
    { 
     // Result includes a Bitmap thumbnail? 
     if (data != null) 
     { 
      if (data.hasExtra("data")) 
      { 
       //Bitmap thumbnail = data.getParcelableExtra("data"); 

       Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 

       mImageView.setImageBitmap(thumbnail); 
      } 
     } 
     // If there is no thumbnail data, the image will have been stored in target output URI. 
     else 
     { 

      Cursor cursor = getContentResolver().query(
        Media.EXTERNAL_CONTENT_URI, new String[] 
          { 
           Media.DATA, 
           Media.DATE_ADDED, 
           MediaStore.Images.ImageColumns.ORIENTATION 
          }, 
          Media.DATE_ADDED, 
          null, 
          "date_added ASC" 
      ); 

      if (cursor != null && cursor.moveToFirst()) 
      { 
       do 
       { 
        mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA))); 
        mPhotoPath = mURI.toString(); 
       } 
       while (cursor.moveToNext()); 
       cursor.close(); 
      } 

      // Resize full image to fit out in image view. 
      int width = mImageView.getWidth(); 
      int height = mImageView.getHeight(); 

      BitmapFactory.Options factoryOptions = new BitmapFactory.Options(); 

      factoryOptions.inJustDecodeBounds = true; 

      BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions); 

      int imageWidth = factoryOptions.outWidth; 
      int imageHeight = factoryOptions.outHeight; 

      // Determine how much to scale down the image 
      int scaleFactor = Math.min(
        imageWidth/width, 
        imageHeight/height 
        ); 

      // Decode the image file into a Bitmap sized to fill view 

      factoryOptions.inJustDecodeBounds = false; 
      factoryOptions.inSampleSize = scaleFactor; 
      factoryOptions.inPurgeable = true; 

      Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions); 

      mImageView.setImageBitmap(bitmap); 
     } 
    } 
} 
} 
+1

嘗試給位圖photo =(位圖)data.getExtras()。get(「data」);在if條件而不是位圖thumbnail = data.getParcelableExtra(「data」); – Droidee 2013-02-28 15:40:52

+0

嘗試過,不工作:(無論如何我加了你的建議 – 2013-02-28 16:20:13

回答

1

我在三星的Android的一些設備同樣的問題,然後我實現的邏輯讓拍攝的照片的路徑

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (requestCode == PICK_IMAGE) 
    { 
     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE); 

     Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC"); 
     if(cursor != null && cursor.moveToFirst()) 
     { 
      do { 
       uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA))); 
       photoPath = uri.toString(); 
      }while(cursor.moveToNext()); 
      cursor.close(); 
     } 

     if(photoPath != null) { 
      Bitmap bitmap = BitmapFactory.decodeFile(photoPath); 
      ///Do Implement your logic whatever you want. 
      mImageView.setImageBitmap(bitmap); 
     } 
    } 
} 
+0

即使在合併你的建議後仍然無法工作:( 我將編輯主線程以顯示整個代碼現在的外觀,太長以至於無法將所有內容都粘貼到此處 – 2013-02-28 16:08:24

+0

另外,我不是在一臺虛擬機上測試這個,而是在一臺Galaxy S3手機上。 – 2013-02-28 16:10:52

+0

我在三星有同樣的問題,請將我的代碼發佈在onActivityResult上,然後你會得到位圖的路徑 – 2013-02-28 16:13:26

0

嘗試增加

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

(和稍微延遲)在

if (requestCode == PICK_IMAGE) 

block。我認爲問題可能是您的設備無法正確刷新Media Store。

(當然是更好的行動是使用MediaScanner到scan your file

1

屢試不爽的銀河S3手機上運行。感謝TGMCians的幫助。

public class MainActivity extends Activity 
{ 
private static final int PICK_IMAGE = 0; 

private static final int PICK_IMAGE_FROM_GALLERY = 1; 

private Button mBtnCamera, mBtnGallery, mBtnCancel; 

private ImageView mImageView; 

private Uri mURI; 
private String mPhotoPath; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mImageView = (ImageView) findViewById(R.id.imgDisplayImage); 

    mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera); 
    mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery); 
    mBtnCancel = (Button) findViewById(R.id.btnCancel); 

    mBtnCamera.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Intent camera = new Intent(); 

      camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE); 

      camera.putExtra("crop", "true"); 

      File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 

      mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg")); 

      camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI); 

      startActivityForResult(camera, PICK_IMAGE); 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (resultCode == Activity.RESULT_OK) 
    { 
     if (requestCode == PICK_IMAGE) 
     { 
      Cursor cursor = getContentResolver().query(
        Media.EXTERNAL_CONTENT_URI, new String[] 
          { 
           Media.DATA, 
           Media.DATE_ADDED, 
           MediaStore.Images.ImageColumns.ORIENTATION 
          }, 
          Media.DATE_ADDED, 
          null, 
          "date_added ASC" 
      ); 

      if (cursor != null && cursor.moveToFirst()) 
      { 
       do 
       { 
        mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA))); 
        mPhotoPath = mURI.toString(); 
       } 
       while (cursor.moveToNext()); 
       cursor.close(); 
      } 
      if (data != null) 
      { 
       if (data.hasExtra("data")) 
       { 
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 

        mImageView.setImageBitmap(thumbnail); 
       } 
       else 
       { 
        System.out.println("Intent bundle does not have the 'data' Extra"); 

        int width = mImageView.getWidth(); 
        int height = mImageView.getHeight(); 

        BitmapFactory.Options factoryOptions = new BitmapFactory.Options(); 

        factoryOptions.inJustDecodeBounds = true; 

        BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions); 

        int imageWidth = factoryOptions.outWidth; 
        int imageHeight = factoryOptions.outHeight; 

        // Determine how much to scale down the image 
        int scaleFactor = Math.min(
          imageWidth/width, 
          imageHeight/height 
          ); 

        // Decode the image file into a Bitmap sized to fill view 

        factoryOptions.inJustDecodeBounds = false; 
        factoryOptions.inSampleSize = scaleFactor; 
        factoryOptions.inPurgeable = true; 

        Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions); 

        mImageView.setImageBitmap(bitmap); 

       } 
      } 
     } 
    } 
    else 
    { 
     System.out.println("Picture taking activity NOT returning RESULT_OK"); 
    } 
} 
} 
相關問題