2012-06-21 48 views
17

Bất kỳ cơ hội nào, có ai biết cách truy cập thư viện ảnh của điện thoại không? Tôi đang tạo một ứng dụng chụp ảnh một chiếc lá cây và phân tích hình ảnh để xác định xem nó có được xác định hay không. Chúng tôi hy vọng rằng chúng tôi có thể cung cấp cho người dùng hai tùy chọn chụp ảnh lá hoặc sử dụng hình ảnh của một chiếc lá mà người dùng đã chụp. Tuy nhiên, chúng tôi đã nhận được hình ảnh tham gia, nhưng chúng tôi không biết cách truy cập thư viện ảnh.Cách truy cập hình ảnh từ thư viện ảnh của điện thoại?

Trả lời

32

Bạn phải khởi chạy Ứng dụng thư viện bằng cách sử dụng được xây dựng trong Intents. Sau đó, trên onActivityResult() của bạn, lấy đường dẫn của hình ảnh được lựa chọn và tải hình ảnh của bạn vào ImageView

bạn main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" 
/> 
<Button 
android:id="@+id/loadimage" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Load Image" 
/> 
<TextView 
android:id="@+id/targeturi" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 
<ImageView 
android:id="@+id/targetimage" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/> 
</LinearLayout> 

Hoạt động của bạn

package com.exercise.AndroidSelectImage; 

    import java.io.FileNotFoundException; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.ImageView; 
    import android.widget.TextView; 

    public class AndroidSelectImage extends Activity { 

    TextView textTargetUri; 
    ImageView targetImage; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button buttonLoadImage = (Button)findViewById(R.id.loadimage); 
     textTargetUri = (TextView)findViewById(R.id.targeturi); 
     targetImage = (ImageView)findViewById(R.id.targetimage); 

     buttonLoadImage.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     Intent intent = new Intent(Intent.ACTION_PICK, 
     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(intent, 0); 
    }}); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK){ 
    Uri targetUri = data.getData(); 
    textTargetUri.setText(targetUri.toString()); 
    Bitmap bitmap; 
    try { 
     bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri)); 
     targetImage.setImageBitmap(bitmap); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 
    } 
    } 

enter image description here

14

Đỗ đừng quên thêm các quyền sau vào AndroidManifest.xml:

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+0

Tôi đang cố gắng đặt hình ảnh đã tải lên làm nền mà bạn biết làm cách nào? – nothingness

+0

READ_EXTERNAL_STORAGE chỉ cần thiết nếu bạn chưa có WRITE_EXTERNAL_STORAGE. Bạn có chắc chắn rằng MANAGE_DOCUMENTS có ý nghĩa không? https://developer.android.com/reference/android/Manifest.permission.html nói: "Quyền này chỉ được yêu cầu bởi ứng dụng quản lý tài liệu nền tảng. Quyền này không thể được cấp cho ứng dụng của bên thứ ba". –

Các vấn đề liên quan