2016-08-15 89 views
0

我的應用程序,爲用戶在註冊時上傳圖片的功能。該圖像將與其他數據一起攜帶到數據庫中。從應用程序的Android工作室中挑選圖片2.1.2

我用的android:在我的XML,而不是上的按鈕實現的onClick監聽器的onClick。在我的設備上運行時沒有任何錯誤,它提供了照片或圖庫選項供您選擇圖像。但是當我選擇圖像時,它不會出現在我爲它設置的ImageView組件中。

XML佈局

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/upload_pic" 
     android:layout_gravity="center" 
     android:text="@string/upload_pic" 
     android:onClick="uploadPic" /> 

    <ImageView 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:layout_gravity="center" 
     android:id="@+id/imageView" /> 

    </LinearLayout> 
在我的java文件

我有試圖實現這一結果的方法。

public void uploadPic(View view){ 
    imageView = (ImageView) findViewById(R.id.imageView); 
    button = (Button) findViewById(R.id.upload_pic); 

    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
    startActivityForResult(gallery, Pick_Image); 
} 



@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode == RESULT_OK && requestCode == Pick_Image){ 
     imageURI = data.getData(); 
     Bitmap bitmap = null; 
     try { 
      bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageURI); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     imageView.setImageBitmap(bitmap); 


    } 

} 
+0

如果您有針對性的Android API 23或greator然後讓你運行的權限? –

+0

什麼是你的Android操作系統版本? –

+0

@SohailZahid Android版本4.4.2 – user3650467

回答

0
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    startActivityForResult(intent,Pick_Image); 

嘗試設置你的位圖:

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

    // Change this line of code 
    if(resultCode == Activity.RESULT_OK && requestCode == Pick_Image){ 

    Uri uri = data.getData(); 
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); 
    imageView.setImageBitmap(bitmap); 

    } 

} 
+0

我試圖抓住你的建議的位圖部分,請看我更新的帖子。它仍然不起作用。還有什麼建議? – user3650467

+0

我更新我的答案。嘗試改變意圖類型。我已經在3-4個項目中使用了這個解決方案,並且它一直工作。 –

+0

仍然無法正常工作,但是這一次它也允許用戶選擇最近照片的選項。但不顯示圖像。 ImageView組件的尺寸能否阻止它顯示出來?用h表示什麼適合使用?該應用程序支持的MINSDK是4.0.3(IceCreamSandwich)我正在測試4.4.2(KitKat) – user3650467

相關問題