我无法从图库或相机中替换其他图像的图像,当我从图库中显示图像并选择图像时,这不会替换图像。此外,当我意图使用相机拍摄图像时,这不显示,我无法拍照。我想替换片段中的图像,这是片段的代码。在相机的情况下,我认为这可能是因为我没有正确添加相机的清单,但后来我看到了这一点,我认为这是正确的。
public class SettingsFragment extends Fragment {
private ImageView ivImage;
private String userChoosenTask;
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private Button btnSelect;
public SettingsFragment() {
}
private void cameraIntent(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
private void galleryIntent(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Selecciona una imagen"), SELECT_FILE);
}
@SuppressWarnings("deprecation")
public void onSelectFromGalleryResult(Intent data){
Bitmap bm = null;
if(data != null){
try{
bm = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(),data.getData());
}catch(IOException e){
e.printStackTrace();
}
}
ivImage.setImageBitmap(bm);
}
private void onCaptureImageResult(Intent data){
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG,90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
FileOutputStream fo;
try{
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
ivImage.setImageBitmap(thumbnail);
}private void SelectImage(){
final CharSequence[] items = {"Realizar foto", "Selecciona una imagen", "Cancelar"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Añadir foto");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result = Utility.checkPermission(getContext());
if(items[item].equals("Realizar una foto")){
userChoosenTask = "Realizar una foto";
if(result)
cameraIntent();
}else if(items[item].equals("Selecciona una imagen")){
userChoosenTask = "Selecciona una imagen";
if(result){
galleryIntent();
}
}else if(items[item].equals("Cancelar")){
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_perfil, container, false);
final Fragment settings = new Sett();
final Fragment datospersonales = new datospersonales();
btnSelect = (Button) rootView.findViewById(R.id.btnSelectPhoto);
final FragmentManager fragmentManager = getFragmentManager();
final ImageButton optsel = (ImageButton) rootView.findViewById(R.id.optsel);
final Button alerta = (Button) rootView.findViewById(R.id.alerta);
btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SelectImage();
}
});
optsel.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, settings).commit();
}
});
alerta.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Notification();
}
});
ivImage = (ImageView) rootView.findViewById(R.id.ivImage);
return rootView;
}
public void Notification(){
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getContext())
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentTitle("C4Growth")
.setContentText("Alerta bullying activada");
NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notificationBuilder.build());
}
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults){
switch (requestCode){
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(userChoosenTask.equals("Realizar una foto")){
cameraIntent();
}else if(userChoosenTask.equals("Selecciona una imagen")){
galleryIntent();
}
}else{
}
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == Activity.RESULT_OK){
if(requestCode == SELECT_FILE){
onSelectFromGalleryResult(data);
}else if(requestCode == REQUEST_CAMERA){
onCaptureImageResult(data);
}
}
}
}
答案 0 :(得分:0)
我一直在看你的代码!在这里你可以用它做什么!把事情搞定了!
第1步: 覆盖onActvityCreated()的方法并在那里使用click监听器并使用父活动的上下文在你的片段中获取它的onActivityResult方法,这样就可以了!
检查这个Stack thread它也有很好的解决方案,我试图告诉它!
第2步:
更多检查此博客我发现它有点帮助 blog link归功于雷克斯圣约翰"为此
注意:试试这个,如果这不起作用,我会尝试与你合作^ _ ^