2016-06-08 69 views
0

我們從我們的應用程序是這樣開始的相機應用:返回沒有得到顯示的ImageView

Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    String timeStamp = "001"; 

    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MymhiImages"); 
    imagesFolder.mkdirs(); 

    File image = new File(imagesFolder, filename + ".png"); 
    Uri uriSavedImage = Uri.fromFile(image); 

,並試圖捕捉從相機這樣的回報:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     InputStream stream = null; 
     if (requestCode == REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK) 
      try { 
       // recyle unused bitmaps 
       if (bitmap != null) { 
        bitmap.recycle(); 
       } 
       stream = getContentResolver().openInputStream(data.getData()); 
       bitmap = BitmapFactory.decodeStream(stream); 

       imageView.setImageBitmap(bitmap); 

       File dir = new File(Environment.getExternalStorageDirectory().toString() + "/mhi/"); 
       if (!dir.exists()) 
        dir.mkdir(); 

       String path = Environment.getExternalStorageDirectory().toString() + "/mhi/" + filename + ".png"; 
       OutputStream out = null; 
       File imageFile = new File(path); 

       try { 
        out = new FileOutputStream(imageFile); 
        // choose JPEG format 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
        out.flush(); 
       } catch (FileNotFoundException e) { 
        // manage exception 
       } catch (IOException e) { 
        // manage exception 
       } finally { 

        try { 
         if (out != null) { 
          out.close(); 
          //jsi.showMsg("Saved Successfully"); 
          Toast.makeText(getApplicationContext(), "Saved Successfully", Toast.LENGTH_LONG).show(); 
         } 

        } catch (Exception exc) { 
        } 

       } 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 

       if (stream != null) 
        try { 
         stream.close(); 
        } catch (IOException ie) { 
         ie.printStackTrace(); 
        } 

      } 
    } 

原諒我們,如果代碼太多的話。

我們的問題是圖像被保存到磁盤,但不顯示在ImageView。我們錯過了什麼?

+0

http://stackoverflow.com/a/37614997/6067866 –

回答

1

ACTION_IMAGE_CAPTURE不返回Uridata.getData()沒用。或者:

  • EXTRA_OUTPUTACTION_IMAGE_CAPTURE的要求,在這種情況下,你知道哪裏有圖像應存放,或

  • 使用data.getExtra("data")獲得縮略圖Bitmap,這是在你做的情況下,返回不提供EXTRA_OUTPUT