我创建了一个意图选择器,用于从图库或相机中挑选任何图像。它在模拟器和其他设备上运行良好,但是当我使用redmi note 4进行测试时,它显示“没有应用程序可以执行此操作”。
代码就在这里 -
try {
f = createImageFile();
outputFileUri = Uri.fromFile(f);
grantUriPermission("com.android.camera", outputFileUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
List<Intent> allIntents = new ArrayList<>();
if (outputFileUri != null) {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> listCam = getPackageManager().queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
if (outputFileUri != null)
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
allIntents.add(intent);
}
}
allIntents.addAll(getGalleryIntents(getPackageManager(), false));
Intent target;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
target = new Intent();
else {
target = allIntents.get(allIntents.size() - 1);
allIntents.remove(allIntents.size() - 1);
}
Intent chooserIntent = Intent.createChooser(target, "Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));
startActivityForResult(chooserIntent, REQ_PICK_IMAGE);
} catch (IOException e) {
e.printStackTrace();
f = null;
outputFileUri = null;
}
public static List<Intent> getGalleryIntents(@NonNull PackageManager packageManager, boolean includeDocuments) {
List<Intent> intents = new ArrayList<>();
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
for (ResolveInfo res : listGallery) {
Intent intent = new Intent(galleryIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
intents.add(intent);
}
if (!includeDocuments) {
for (Intent intent : intents) {
if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
intents.remove(intent);
break;
}
}
}
return intents;
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File albumF = getAlbumDir();
File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, albumF);
return imageF;
}
abstract class AlbumStorageDirFactory {
public abstract File getAlbumStorageDir(String albumName);
}
public final class BaseAlbumDirFactory extends AlbumStorageDirFactory {
// Standard storage location for digital camera files
private static final String CAMERA_DIR = "/dcim/";
@Override
public File getAlbumStorageDir(String albumName) {
return new File(Environment.getExternalStorageDirectory() + CAMERA_DIR + albumName);
}
}
public final class FroyoAlbumDirFactory extends AlbumStorageDirFactory {
@Override
public File getAlbumStorageDir(String albumName) {
return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
}
}
private File getAlbumDir() {
File storageDir = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
storageDir = mAlbumStorageDirFactory.getAlbumStorageDir(getAlbumName());
if (storageDir != null)
if (!storageDir.mkdirs())
if (!storageDir.exists()) {
Log.d("CameraSample", "failed to create directory");
return null;
}
} else Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
return storageDir;
}
请帮忙! 我错过了什么吗?
答案 0 :(得分:0)
我遇到了同样的问题,但后来我意识到这是存储权限问题。我通过添加if条件来修改我的代码。
试试这个
image_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isImageSelect =false;
//checking if permission is granted for write_external_storage
if(ActivityCompat.checkSelfPermission(NewMemberActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
selectImage();
}
else{
//Requesting Permission for write_external_storage
ActivityCompat.requestPermissions(NewMemberActivity.this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
}
});