2014-10-21 56 views
0

因此,昨晚我以爲我有我的應用程序工作,但是,今天早上gremlins已入侵,現在已經使我的應用程序無法正常工作的功能。照片不在圖像視圖中顯示android

基本上,一個按鈕允許用戶拍照,並在Imageview中顯示該照片,然後將該圖像附加到電子郵件中。它讓我拍攝照片,它仍然作爲附件呈現,但圖像視圖中的預覽完全是空的。

任何人都可以看到發生了什麼?

Hoon_Image = (ImageView) findViewById(R.id.CapturedImage); 
     button_take_photo = (Button)findViewById(R.id.btn_take_photo); 
     button_take_photo.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       try { 
        f = createImageFile(); 
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
        startActivityForResult(cameraIntent, CAMERA_REQUEST); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 



      } 
     }); 
public File getAlbumDir() 
    { 

     File storageDir = new File(
       Environment.getExternalStoragePublicDirectory(
         Environment.DIRECTORY_PICTURES 
       ), 
       "BAC/" 
     ); 
     // Create directories if needed 
     if (!storageDir.exists()) { 
      storageDir.mkdirs(); 
     } 

     return storageDir; 
    } 
    private File createImageFile() throws IOException { 
     // Create an image file name 

     String imageFileName =getAlbumDir().toString() +"/image.jpg"; 
     File image = new File(imageFileName); 
     return image; 
    } 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if(requestCode == CAMERA_REQUEST){ 
       Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath()); 
       Hoon_Image.setImageBitmap(photo); 
      } 
     } 
     if (resultCode == RESULT_OK) { 
      if (requestCode == SELECT_PICTURE) { 
       Uri selectedImageUri = data.getData(); 

       //OI FILE Manager 
       filemanagerstring = selectedImageUri.getPath(); 

       //MEDIA GALLERY 
       selectedImagePath = getPath(selectedImageUri); 

       //DEBUG PURPOSE - you can delete this if you want 
       if(selectedImagePath!=null) 
        System.out.println(selectedImagePath); 
       else System.out.println("selectedImagePath is null"); 
       if(filemanagerstring!=null) 
        System.out.println(filemanagerstring); 
       else System.out.println("filemanagerstring is null"); 

       //NOW WE HAVE OUR WANTED STRING 
       if(selectedImagePath!=null) 
        System.out.println("selectedImagePath is the right one for you!"); 
       else 
        System.out.println("filemanagerstring is the right one for you!"); 
      } 
      Bitmap photo = BitmapFactory.decodeFile(selectedImagePath); 
      Hoon_Image.setImageBitmap(photo); 
      Photo_Selected = 1; 
     } 
    } 

回答

0

因此,我發現我在做什麼錯 - 銳化時刻來了。

這部分代碼:

if (resultCode == RESULT_OK) { 
      if(requestCode == CAMERA_REQUEST){ 

它只是在尋找好的結果(即發生都選擇照片和照片到成功),而不是首先尋找RequestCode的。

重新安排代碼這個工作:

if(requestCode == CAMERA_REQUEST) { 
      if (resultCode == RESULT_OK){ 
       Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath()); 
       Hoon_Image.setImageBitmap(photo); 
0

我已經對圖像進行什麼在Android上的ImageView被示出的是使用一種方法decodeSampledBitmapFromFile(字符串路徑,INT reqWidth,INT reqHeight),其具有所需的寬度和高度在文件對象進行解碼。以下是一個示例代碼。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == RESULT_OK) { 
     File file = new File(Environment.getExternalStorageDirectory() 
       + File.separator + "image.jpg"); 
     bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 
       600, 450); 
     imageView.setImageBitmap(bitmap); 

    } 
} 

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, 
     int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    // Query bitmap without allocating memory 
    options.inJustDecodeBounds = true; 
    // decode file from path 
    BitmapFactory.decodeFile(path, options); 
    // Calculate inSampleSize 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    // decode according to configuration or according best match 
    options.inPreferredConfig = Bitmap.Config.RGB_565; 
    int inSampleSize = 1; 
    if (height > reqHeight) { 
     inSampleSize = Math.round((float) height/(float) reqHeight); 
    } 
    int expectedWidth = width/inSampleSize; 
    if (expectedWidth > reqWidth) { 
     // if(Math.round((float)width/(float)reqWidth) > inSampleSize) // 
     // If bigger SampSize.. 
     inSampleSize = Math.round((float) width/(float) reqWidth); 
    } 
    // if value is greater than 1,sub sample the original image 
    options.inSampleSize = inSampleSize; 
    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(path, options); 
} 
+0

這並不完全回答我的問題雖然。 – scb998 2014-10-22 01:41:00

+0

但ImageView已經顯示圖像? – avinea28 2014-10-22 01:44:45

+0

我上面的代碼在前一天晚上工作,今天早上它不是。如果我能避免它,我不想重新編寫我的應用程序。 – scb998 2014-10-22 01:50:48

相關問題