2014-09-30 55 views
0

我試圖從庫中讀取的圖像,然後使用該圖像輸入straming,下面是我的代碼如何採取圖庫圖像作爲輸入流

@Override 
public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mGetImageButton = (Button) findViewById(R.id.button_getImage); 
     mGetImageButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // SET action AND miniType 
       Intent intent = new Intent(); 
       intent.setAction(Intent.ACTION_PICK); 
       intent.setType("image/*"); 
       // REQUEST Uri of image 
       startActivityForResult(intent, REQUEST_IMAGE); 
      } 
     }); 
     mImageViewForGallery = (ImageView) findViewById(R.id.imageView2); 
    } 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      super.onActivityResult(requestCode, resultCode, data); 
      if (resultCode != Activity.RESULT_OK) {return;} 

      if (requestCode == REQUEST_IMAGE) { 
       Uri uri = data.getData(); 
       // SET image 
       mImageViewForGallery.setImageURI(uri); 
       Drawable drawable = mImageViewForGallery.getDrawable(); 
       InputStream is; 
       is = this.getResources().openRawResource(R.drawable.img1); 
       Bitmap bmInImg = BitmapFactory.decodeStream(is); 

      } 
     } 

在上面的代碼中is = this.getResources().openRawResource(R.drawable.img1);是閱讀從繪製的文件夾名稱圖像IMG1,但現在我的形象是我的畫廊,我怎麼能接受圖像作爲輸入流,當我試着像

InputStream is; 
is=uri; 

但其顯示的錯誤選擇圖像,以及是從C對Java ++。

編輯,@Shawn答案後,我把這個代碼在onActivityResult功能線Drawable drawable = mImageViewForGallery.getDrawable();後:

 InputStream is = this.getContentResolver().openInputStream(uri); 
     Bitmap bmInImg = BitmapFactory.decodeStream(is); 
     InputStream Vign = this.getResources().openRawResource(R.drawable.p); 
     Bitmap bmInImg2 = BitmapFactory.decodeStream(Vign); 
     mPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()]; 
     nPhotoIntArray = new int[bmInImg.getWidth() * bmInImg.getHeight()]; 
     vPhotoIntArray = new int[bmInImg2.getWidth() * bmInImg2.getHeight()]; 

但它顯示我下面的錯誤上this.getContentResolver().openInputStream(uri);

錯誤:

Unhandled exception type FileNotFoundException 

錯誤當我在代碼中使用InputStream Vign = this.getResources().openRawResource(R.drawable.p);時出現。

回答

0

,如果你有開放的我們的形象,你的內容解析器解析它:

InputStream is = context.getContentResolver().openInputStream(uri); 

不要忘記關閉流。 (並檢查爲空)。上下文有時候是你的活動或「這個」。

+0

但是,當我使用代碼'InputStream Vign = this.getResources()。openRawResource(R.drawable.p);'在這行下面你提到它顯示錯誤,否則它不顯示錯誤,爲什麼 – AHF 2014-09-30 19:54:58

+0

...你已經獲得了輸入流,爲什麼你仍然需要openRawResource?只是解碼bitmapfactory – Shawn 2014-09-30 19:57:26

+0

是的,但我想要另一個圖像使用inputstream,這是可繪製 – AHF 2014-09-30 19:59:54