Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 未调用onActivityResult()。我能';不要展示图片_Android_Onactivityresult - Fatal编程技术网

Android 未调用onActivityResult()。我能';不要展示图片

Android 未调用onActivityResult()。我能';不要展示图片,android,onactivityresult,Android,Onactivityresult,我正在使用Intent呼叫照相机和画廊。 我调用camera或gallery,但未调用onActivityResult()。 我可以拍照并打开图库,但我无法显示图片。 我应该在哪里修理? 请教我 public static class SelectImageDialog extends DialogFragment{ @Override public Dialog onCreateDialog(Bundle savedInstanceState){

我正在使用Intent呼叫照相机和画廊。 我调用camera或gallery,但未调用onActivityResult()。 我可以拍照并打开图库,但我无法显示图片。 我应该在哪里修理? 请教我

    public static class SelectImageDialog extends DialogFragment{
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState){
            AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
            builder.setMessage("please select").setPositiveButton("camera",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent=new Intent();
                    intent.setAction("android.media.action.IMAGE_CAPTURE");
                    startActivityForResult(intent,CAMERA);
                }
            }).setNegativeButton("gallery",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent=new Intent();
                    intent.setAction(intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    startActivityForResult(intent,GALLERY);
                }
            });
            return builder.create();
        }
    }

    @Override
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        if(requestCode==CATEGORY){
            if(resultCode==RESULT_OK) {
                Bundle bundle = data.getExtras();
                textCategory.setText(bundle.getString("item"));
            }
        }else if(requestCode==GALLERY){
            if(resultCode==RESULT_OK){
                try{
                    InputStream is=getContentResolver().openInputStream(data.getData());
                    Bitmap bitmap=BitmapFactory.decodeStream(is);
                    imageButton.setImageBitmap(bitmap);
                    is.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }else if(requestCode==CAMERA){
            if(resultCode==RESULT_OK){
                Bitmap bitmap=(Bitmap)data.getExtras().get("data");
                imageButton.setImageBitmap(savePic(bitmap));
            }
        }
    }

    public Bitmap savePic(Bitmap bitmap){
        String path= Environment.getExternalStorageDirectory().getPath();
        String fileName = System.currentTimeMillis() + ".png";
        try {
            FileOutputStream outputStream = new FileOutputStream(path+"/"+fileName);
            bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        String[] paths = {Environment.getExternalStorageDirectory().getPath()+"/"+fileName};
        String[] mimeType = {"image/png"};
        MediaScannerConnection.scanFile(getApplicationContext(), paths, mimeType, new MediaScannerConnection.OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String s, Uri uri) {
            }
        });
        return bitmap;
    }

这就是我对照相机的称呼

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath(); 
Uri uri = Uri.fromFile(fileName );
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri );
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);

这就是我对照相机的称呼

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempFile = File.createTempFile("my_app", ".jpg");
fileName = tempFile.getAbsolutePath(); 
Uri uri = Uri.fromFile(fileName );
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri );
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);

您可以通过以下方式找到解决方案:

final CharSequence[] options = { "Take Photo", "Choose from Gallery",
            "Cancel" };
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Select Profile Photo from");
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {

            if (options[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(intent, 1);
            }

            else if (options[item].equals("Choose from Gallery")) {
                Intent intent;
                if (Build.VERSION.SDK_INT > 19) {
                    intent = new Intent(
                            Intent.ACTION_GET_CONTENT,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                } else {
                    intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                }

                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 0);
                intent.putExtra("aspectY", 0);
                intent.setType("image/*");
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 150);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "gallery.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                intent.putExtra("return-data", true);
                startActivityForResult(intent, 2);
            } else if (options[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }

    });
    builder.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                previewCapturedImage("temp.jpg");

            } else if (requestCode == 2) {

                if (Build.VERSION.SDK_INT > 19) {
                    Uri selectedImage = data.getData();

                    setImageBitmapfromPath(getPath(selectedImage));

                } else {
                    previewCapturedImage("gallery.jpg");

                }

            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@SuppressLint("NewApi")
private String getPath(Uri uri) {
    if (uri == null) {
        return null;
    }

    String[] projection = { MediaStore.Images.Media.DATA };

    Cursor cursor;
    if (Build.VERSION.SDK_INT > 19) {

        String wholeID = DocumentsContract.getDocumentId(uri);

        String id = wholeID.split(":")[1];

        String sel = MediaStore.Images.Media._ID + "=?";

        cursor = getActivity().getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
                sel, new String[] { id }, null);
    } else {
        cursor = getActivity().getContentResolver().query(uri, projection,
                null, null, null);
    }
    String path = null;
    try {
        int column_index = cursor
                .getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        path = cursor.getString(column_index).toString();
        cursor.close();
    } catch (NullPointerException e) {

    }
    return path;
}

@SuppressWarnings("deprecation")
private void setImageBitmapfromPath(String picturePath) {
    try {
        if (picturePath != null) {
            bitmap = decodeFile(new File(picturePath));
            Bitmap localBitmap1 = new RoundedTransformation(8, 0)
                    .transform(bitmap);
            ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
            localBitmap1.compress(Bitmap.CompressFormat.PNG, 10,
                    localByteArrayOutputStream);
            Bitmap localBitmap2 = BitmapFactory
                    .decodeStream(new ByteArrayInputStream(
                            localByteArrayOutputStream.toByteArray()));
            BitmapDrawable localBitmapDrawable = new BitmapDrawable(
                    getResources(), localBitmap2);

            iv_user_photo.setBackgroundDrawable(localBitmapDrawable);
            strImagepath = picturePath;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}




private void previewCapturedImage(String fName) {
    File f = new File(Environment.getExternalStorageDirectory().toString());
    for (File temp : f.listFiles()) {
        if (temp.getName().equals(fName)) {
            f = temp;
            break;
        }
    }
    setImageBitmapfromPath(f.getAbsolutePath());
}`

您可以通过以下方式找到解决方案:

final CharSequence[] options = { "Take Photo", "Choose from Gallery",
            "Cancel" };
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Select Profile Photo from");
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {

            if (options[item].equals("Take Photo")) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                startActivityForResult(intent, 1);
            }

            else if (options[item].equals("Choose from Gallery")) {
                Intent intent;
                if (Build.VERSION.SDK_INT > 19) {
                    intent = new Intent(
                            Intent.ACTION_GET_CONTENT,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                } else {
                    intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                }

                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 0);
                intent.putExtra("aspectY", 0);
                intent.setType("image/*");
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 150);
                File f = new File(android.os.Environment
                        .getExternalStorageDirectory(), "gallery.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                intent.putExtra("return-data", true);
                startActivityForResult(intent, 2);
            } else if (options[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }

    });
    builder.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                previewCapturedImage("temp.jpg");

            } else if (requestCode == 2) {

                if (Build.VERSION.SDK_INT > 19) {
                    Uri selectedImage = data.getData();

                    setImageBitmapfromPath(getPath(selectedImage));

                } else {
                    previewCapturedImage("gallery.jpg");

                }

            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@SuppressLint("NewApi")
private String getPath(Uri uri) {
    if (uri == null) {
        return null;
    }

    String[] projection = { MediaStore.Images.Media.DATA };

    Cursor cursor;
    if (Build.VERSION.SDK_INT > 19) {

        String wholeID = DocumentsContract.getDocumentId(uri);

        String id = wholeID.split(":")[1];

        String sel = MediaStore.Images.Media._ID + "=?";

        cursor = getActivity().getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
                sel, new String[] { id }, null);
    } else {
        cursor = getActivity().getContentResolver().query(uri, projection,
                null, null, null);
    }
    String path = null;
    try {
        int column_index = cursor
                .getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        path = cursor.getString(column_index).toString();
        cursor.close();
    } catch (NullPointerException e) {

    }
    return path;
}

@SuppressWarnings("deprecation")
private void setImageBitmapfromPath(String picturePath) {
    try {
        if (picturePath != null) {
            bitmap = decodeFile(new File(picturePath));
            Bitmap localBitmap1 = new RoundedTransformation(8, 0)
                    .transform(bitmap);
            ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
            localBitmap1.compress(Bitmap.CompressFormat.PNG, 10,
                    localByteArrayOutputStream);
            Bitmap localBitmap2 = BitmapFactory
                    .decodeStream(new ByteArrayInputStream(
                            localByteArrayOutputStream.toByteArray()));
            BitmapDrawable localBitmapDrawable = new BitmapDrawable(
                    getResources(), localBitmap2);

            iv_user_photo.setBackgroundDrawable(localBitmapDrawable);
            strImagepath = picturePath;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}




private void previewCapturedImage(String fName) {
    File f = new File(Environment.getExternalStorageDirectory().toString());
    for (File temp : f.listFiles()) {
        if (temp.getName().equals(fName)) {
            f = temp;
            break;
        }
    }
    setImageBitmapfromPath(f.getAbsolutePath());
}`

检查:您是否也在承载
SelectImageDialog
的活动中覆盖了
onActivityResult
?如果是真的,请也粘贴该代码。对不起,我的答复太晚了。我可以解决这个问题。很高兴知道。谢谢你。@tarofess,你能写下你是怎么解决的吗?你接受了答案,但仍然提到你已经解决了它。您是用接受的答案还是通过其他方式解决的?检查:您是否也在承载
SelectImageDialog
的活动中覆盖了
onActivityResult
?如果是真的,请也粘贴该代码。对不起,我的答复太晚了。我可以解决这个问题。很高兴知道。谢谢你。@tarofess,你能写下你是怎么解决的吗?你接受了答案,但仍然提到你已经解决了它。你是用公认的答案还是用其他方法解决的?对不起,我的答复晚了。我可以解决这个问题。很高兴知道。谢谢。对不起,我的答复晚了。我可以解决这个问题。很高兴知道。谢谢。对不起,我的答复晚了。我可以解决这个问题。很高兴知道。谢谢。对不起,我的答复晚了。我可以解决这个问题。很高兴知道。谢谢,欢迎。你需要回答这个问题,如果这不是一个答案,或者发表评论,但更多的是一个非正式的线索,如这里。看见然而,对这个问题发表评论需要更多的声誉。欢迎。你需要回答这个问题,如果这不是一个答案,或者发表评论,但更多的是一个非正式的线索,如这里。看见然而,就这个问题发表评论需要更多的声誉。