Hướng dẫn sử dụng Intent filter để nhận dữ liệu vào ứng dụng android

View 03cd82 2024

1.    Tạo activity mới hoặc tạo project mới
2.    Vào file manifest khai báo intent-filter ở trong thẻ activity tương ứng

<intent-filter>
    <action android:name="android.intent.action.SEND"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="image/*"/>
</intent-filter>

3.    Vào file layout tạo 1 image view mới:

<ImageView
    android:id="@+id/img_shared"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

4.    Vào activity tham chiếu tới image gắn dữ liệu hình ảnh mà intent nhận được

ImageView imageView = findViewById(R.id.img_shared);

imageView.setImageURI((Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM));

Biên dịch chương trình cài đặt vào máy, tạm thời không chạy ứng  dụng mà hãy mở 1 ứng dụng quản lý ảnh có sẵn trên điện thoại và bấm xem ảnh rồi bấm chia sẻ, sẽ nhìn thấy ứng dụng, chia sẻ vào ứng dụng sẽ hiển thị ảnh. 
==> Thành công gửi ảnh vào ứng dụng. 
 

Nguồn: zezo.dev