、ActivityResult上
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
//Define Button in the Xml file and get them
Button galleryButton= (Button)findViewById(R.id.gallerybutton);
Button cameraButton= (Button)findViewById(R.id.camerabutton);
//Listener's on the button
galleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, PICK_FROM_GALLARY);
}
});
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Camera permission required for Marshmallow version
// permission has been granted, continue as usual
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
}
}
});
そして、マシュマロのOSで使用すると、画像を取得します//ランタイムPermisionの場合のように
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLARY = 2;
Uri outPutfileUri;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK) {
//pic coming from camera
Bitmap bitmap=null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), outPutfileUri);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case PICK_FROM_GALLARY:
if (resultCode == Activity.RESULT_OK) {
//pick image from gallery
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(imgDecodableString);
}
break;
}
}
は、//マシュマロデバイスのAndroid 7.0
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Camera permission required for Marshmallow version
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Callback onRequestPermissionsResult interceptadona Activity MainActivity
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, PICK_FROM_CAMERA);
} else {
// permission has been granted, continue as usual
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
}
}
});
//Gallery storage permission required for Marshmallow version
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
//のためには、一年のほぼ半分の後その
ContentValues values = new ContentValues(1);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
outPutfileUri = getActivity().getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
もちろん、コードを表示してどこが間違っているかを正確に伝える必要があります。あなたはgetPathFromURIカーソルのようなものなしですべきです。あなただけが良いコードを持っています。多くのコードを投稿しないことをお勧めします。 1つのインテントから始めます。 – greenapps