Android 导入手机图片

用户行为

  • 用户使用手机拍摄照片
  • 用户下载网页上的照片,网盘的照片,微博的照片到手机
  • 用户导入照片到SD卡

需求

  • 当用户发生上述行为的时候,将增加的照片导入到程序并上传

实现思路

监听相机应用

如果用户拍摄了照片,可能会触发一个Intent,发送广播通知给其他程序

public static final String ACTION_NEW_PICTURE

Added in API level 14

Broadcast Action: A new picture is taken by the camera, and the entry of the picture has been added to the media store. getData() is URI of the picture.

Constant Value: "android.hardware.action.NEW_PICTURE"

public static final String ACTION_NEW_VIDEO

Added in API level 14

Broadcast Action: A new video is recorded by the camera, and the entry of the

video has been added to the media store. getData() is URI of the video.

Constant Value: "android.hardware.action.NEW_VIDEO"

代码

public class CameraEventReciver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Cursor cursor = context.getContentResolver().query(intent.getData(),null,null, null, null);
    cursor.moveToFirst();
    String image_path = cursor.getString(cursor.getColumnIndex("_data"));
    Toast.makeText(context, "New Photo is Saved as : -" + image_path, 1000).show();
  }
}

 <receiver
        android:name="sample.grass.category.mediastore.CameraEventReciver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.hardware.action.NEW_PICTURE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />

        </intent-filter>
    </receiver>

测试结果:
能够监听到用户拍摄了照片,但是仅限于系统相机,使用其他的相机apk可能收到广播,也有可能收不到

优点

  • 即使应用程序没有启动,也能接收到通知,从而被唤醒

缺点

  • 有的相机应用并没有实现发送广播,不是完全可以被依赖
  • 无法监测用户下载图片或者导入照片

监听Meida数据库

无论是用户使用哪个相机拍摄照片,还是用户导入图片到sd卡,还是用户下载照片到手机,都会引起Media 数据库的变化,监听这个变化,导入照片到Trunx

代码

class UriObserver extends ContentObserver {
    public UriObserver(Handler handler) {
        super(handler);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onChange(boolean selfChange) {
        // TODO Auto-generated method stub
        super.onChange(selfChange);

        Log.d("INSTANT", "GETTING CHANGES");
    }
}

UriObserver observer = new UriObserver(new Handler());
this.getApplicationContext()
    .getContentResolver()
    .registerContentObserver(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false,
    observer);

优点

  • 可以监听用户导入图片,用户下载图片,用户使用各种第三方相机拍摄图片

缺点

  • 依赖于程序被启动,如果程序被杀掉了,就无法监听了

监听文件目录

代码

FileObserver observer = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA") { // set up a file observer to watch this directory on sd card
	            @Override
	        public void onEvent(int event, String file) {
	            if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is launched
	                Log.d(TAG, "File created [" + android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA/" + file + "]");
	                fileSaved = "New photo Saved: " + file;
	            }
	        }
	    };
observer.startWatching(); // start the observer

优点

  • 能够监听到上述的所有的用户行为

缺点

  • 需要硬编码,并且监听的目录太多,事倍功半

参考

原文地址:https://www.cnblogs.com/idealgrass/p/4454226.html