Android排除一些相机意图 [英] Android Exclude Some Camera Intent
问题描述
我正在开发一个应用程序,需要使用从WebView输入的文件使用摄像头。
所以这是我写的代码,它是工作
webView.setWebChromeClient (new WebChromeClient()
{
//无文档的魔法方法覆盖
//如果你尝试把@Override放在这里,Eclipse会发出你的
// For Android 3.0+
@SuppressWarnings(unused)
public void openFileChooser(ValueCallback< Uri> uploadMsg){
mUploadMessage = uploadMsg;
文件imageStorageDir = new文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),MyApp);
//如果不存在则创建存储目录
if(!imageStorageDir.exists()){
imageStorageDir .mkdirs();
}
文件文件=新文件(imageStorageDir + File.separator +IMG_+ String.valueOf(System.currentTimeMillis())+.jpg);
imageUri = Uri.fromFile(file);
final List< Intent> cameraIntents = new ArrayList< intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List< ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent,0);
for(ResolveInfo res:listCam){
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
cameraIntents.add(i);
}
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(image / *);
Intent chooserIntent = Intent.createChooser(i,Image Chooser);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,cameraIntents.toArray(new Parcelable [] {}));
MainActivity.this.startActivityForResult(chooserIntent,FILECHOOSER_RESULTCODE);
}
//对于Android 4.1
@SuppressWarnings(未使用)
public void openFileChooser(ValueCallback< Uri> uploadMsg,String acceptType) {
mUploadMessage = uploadMsg;
文件imageStorageDir =新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),MyApp);
//如果不存在则创建存储目录
if(!imageStorageDir.exists()){
imageStorageDir.mkdirs();
}
文件文件=新文件(imageStorageDir + File.separator +IMG_+ String.valueOf(System.currentTimeMillis())+.jpg);
imageUri = Uri.fromFile(file);
final List< Intent> cameraIntents = new ArrayList< Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List< ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent,0);
for(ResolveInfo res:listCam){
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
cameraIntents.add(i);
}
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(image / *);
Intent chooserIntent = Intent.createChooser(i,Image Chooser);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,cameraIntents.toArray(new Parcelable [] {}));
MainActivity.this.startActivityForResult(chooserIntent,FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
@SuppressWarnings(unused)
public void openFileChooser(ValueCallback< Uri> uploadMsg,String acceptType ,String capture){
mUploadMessage = uploadMsg;
文件imageStorageDir =新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),MyApp);
//如果不存在则创建存储目录
if(!imageStorageDir.exists()){
imageStorageDir.mkdirs();
}
文件文件=新文件(imageStorageDir + File.separator +IMG_+ String.valueOf(System.currentTimeMillis())+.jpg);
imageUri = Uri.fromFile(file);
final List< Intent> cameraIntents = new ArrayList< Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List< ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent,0);
for(ResolveInfo res:listCam){
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
cameraIntents.add(i);
}
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(image / *);
Intent chooserIntent = Intent.createChooser(i,Image Chooser);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,cameraIntents.toArray(new Parcelable [] {}));
MainActivity.this.startActivityForResult(chooserIntent,FILECHOOSER_RESULTCODE);
}
//对于Android 5.0+
public boolean onShowFileChooser(
WebView webView,ValueCallback< Uri []> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams){
//检查我们没有任何现有的回调
if(mFilePathCallback!= null){
mFilePathCallback.onReceiveValue(null) ;
}
mFilePathCallback = filePathCallback;
//设置拍照目的
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager())!= null){
//创建照片应该去的文件
文件photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra(PhotoPath,mCameraPhotoPath);
} catch(IOException ex){
//创建文件时发生错误
Log.e(tag,无法创建映像文件,ex);
}
//仅当文件成功创建时继续
if(photoFile!= null){
mCameraPhotoPath =file:+ photoFile.getAbsolutePath );
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
//设置获取现有图像的意图
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType(image / *);
//设置Intent选择器的意图
Intent [] intentArray;
if(takePictureIntent!= null){
intentArray = new Intent [] {takePictureIntent};
} else {
intentArray = new Intent [0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT,contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE,Image Chooser);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,intentArray);
startActivityForResult(chooserIntent,MainActivity.FILECHOOSER_RESULTCODE);
return true;
}
});
这是我的活动结果:
private Uri imageUri;
@Override
protected void onActivityResult(int requestCode,int resultCode,
Intent intent){
if(Build.VERSION.SDK_INT> = 21){
if (requestCode!= FILECHOOSER_RESULTCODE || mFilePathCallback == null){
super.onActivityResult(requestCode,resultCode,intent);
return;
}
Uri [] results = null;
//检查响应是否良好
if(resultCode == Activity.RESULT_OK){
if(intent == null){
//如果没有数据,那么我们可能已经拍了照片
if(mCameraPhotoPath!= null){
results = new Uri [] {Uri.parse(mCameraPhotoPath)};
}
} else {
String dataString = intent.getDataString();
if(dataString!= null){
results = new Uri [] {Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
return;
} else {
if(requestCode == FILECHOOSER_RESULTCODE)
{
if(null == this.mUploadMessage){
return;
}
Uri结果;
if(resultCode!= RESULT_OK){
result = null;
} else {
result = intent == null? this.imageUri:intent.getData(); //如果意图为null,从私有变量中检索
}
this.mUploadMessage.onReceiveValue(result);
this.mUploadMessage = null;
}
}
}
我的问题是如何在输入动作时排除某些相机意图?
例如我想删除相机360或retrica在列表中,怎么办?
编辑此部分:
for(ResolveInfo res :listCam){
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
cameraIntents.add(i);
}
到这一个:
for(ResolveInfo res:listCam){
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
if(packageName.equals(ihate.this.package)|| packageName.equals(begone.unwanted.package)){
Log.i(camera,res.activityInfo。 packageName +blocked!);
} else {
i.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));
captureIntent.setPackage(packageName);
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
cameraIntents.add(i);
}
}
如果您想知道包name只是记录res.activityInfo.packageName
您可以对android 5.0使用相同的方法
I'm developing an app that will need to use a camera using the file input from WebView.
So this is the code that I write and it is working with the google camera.
In my webchromeclient
webView.setWebChromeClient(new WebChromeClient()
{
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// For Android 3.0+
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
// Create the storage directory if it does not exist
if (! imageStorageDir.exists()){
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
imageUri = Uri.fromFile(file);
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntents.add(i);
}
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
//For Android 4.1
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){
mUploadMessage = uploadMsg;
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
// Create the storage directory if it does not exist
if (! imageStorageDir.exists()){
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
imageUri = Uri.fromFile(file);
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntents.add(i);
}
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
//For Android 3.0+
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
// Create the storage directory if it does not exist
if (! imageStorageDir.exists()){
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
imageUri = Uri.fromFile(file);
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntents.add(i);
}
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
//For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if(mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
// Set up the take picture intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(tag, "Unable to create Image File", ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
// Set up the intent to get an existing image
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
// Set up the intents for the Intent chooser
Intent[] intentArray;
if(takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, MainActivity.FILECHOOSER_RESULTCODE);
return true;
}
});
Then this is my activityresult:
private Uri imageUri;
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (Build.VERSION.SDK_INT >= 21){
if(requestCode != FILECHOOSER_RESULTCODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
Uri[] results = null;
// Check that the response is a good one
if(resultCode == Activity.RESULT_OK) {
if(intent == null) {
// If there is not data, then we may have taken a photo
if(mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else {
String dataString = intent.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
return;
}else{
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == this.mUploadMessage) {
return;
}
Uri result;
if (resultCode != RESULT_OK) {
result = null;
} else {
result = intent == null ? this.imageUri : intent.getData(); // retrieve from the private variable if the intent is null
}
this.mUploadMessage.onReceiveValue(result);
this.mUploadMessage = null;
}
}
}
It is working with image upload or taking photo using the google camera.
My Question is how to exclude some camera intent when making the input action?
For example I want to remove camera 360 or retrica in the list, how to do it?
Edit this part:
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntents.add(i);
}
To This one:
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent i = new Intent(captureIntent);
if(packageName.equals("ihate.this.package")||packageName.equals("begone.unwanted.package")){
Log.i("camera", res.activityInfo.packageName+" blocked!");
}else{
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
captureIntent.setPackage(packageName);
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
cameraIntents.add(i);
}
}
If you want to know the package name just Log the res.activityInfo.packageName
You can do the same approach for android 5.0
这篇关于Android排除一些相机意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!