Я привел несколько примеров из SO о том, как извлечь изображение из камеры или Gallary. Часть камеры работает, но галактическая часть не работает. Кодекс кажется очень diffuclt, чтобы понять для меня, поэтому я не знаю, что именно ухаживать.
У меня также есть необходимые разрешения в моем манифесте.
Вот видео проблемы: https://www.youtube.com/watch?v=OOoY1y4W86w
ImagePicker (просмотр V), намерение/выборщики, файлы, URIS
public void ImagePicker(View v) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
final File rootdir = new File(Environment.getExternalStorageDirectory() + File.separator + "TravelDiary" + File.separator);
rootdir.mkdirs();
final String filename = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirecotry = new File(rootdir, filename);
outputFileUri = Uri.fromFile(sdImageMainDirecotry);
Log.d("TAG", "IM HERE 1");
//camera
final List<Intent> cameraIntents = new ArrayList<>();
final Intent CameraCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listcam = packageManager.queryIntentActivities(CameraCaptureIntent, 0);
Log.d("TAG", "IM HERE 2");
for (ResolveInfo res : listcam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(CameraCaptureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
Log.d("TAG", "IM HERE 3");
}
//Gallary
final Intent imageChooser = new Intent();
imageChooser.setType("image/*");
imageChooser.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(imageChooser, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, SELECT_FROM_GALLARY);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
} else {
Toast.makeText(this, "External storage not available", Toast.LENGTH_SHORT).show();
}
}
onActivityResult():
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_FROM_GALLARY) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
selectedImageUri = outputFileUri;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(selectedImageUri.getPath(), options);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
pic.setBackground(drawable);
} else {
selectedImageUri = data == null ? null : data.getData();
Log.d("ImageURI", selectedImageUri.getLastPathSegment());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
try {
InputStream input = getContentResolver().openInputStream(selectedImageUri);
final Bitmap bitmap = BitmapFactory.decodeStream(input, null, options);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
pic.setBackground(drawable);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}