图像视图不会显示图像setImageBitmap()

时间:2016-09-25 09:47:48

标签: android

这适用于模拟器(我使用genymotion)但由于某种原因它不能在真实设备上工作。 图像视图不会显示图像,我确定它不是空的,因为它显示路径文件。这是代码。当我启动应用程序时,一切正常,但文件(图像)没有显示在图像视图上。我试着查看logcat,并且没有关于图像为什么不能显示的错误。谢谢你的回答

val content = scala.io.Source.fromURL("http://ichart.finance.yahoo.com/table.csv?s=FB").mkString

val list = content.split("\n").filter(_ != "")

val rdd = sc.parallelize(list)

val df = rdd.toDF

方法startDialog()

ImageView img_logo;
    protected static final int CAMERA_REQUEST = 1;
    protected static final int FILE_REQUEST = 2;
    private Uri imageCaptureUri;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_my_account, container, false);
        try{
        firstname = getArguments().getString("firstname");
        lastname = getArguments().getString("lastname");
        username = getArguments().getString("username");
        cno = getArguments().getString("cno");
        email = getArguments().getString("email");
        address = getArguments().getString("address");
        userID = getArguments().getString("userID");

        fullname = (TextView) view.findViewById(R.id.name);
        tv_uname = (TextView) view.findViewById(R.id.user_name);
        tv_cno = (TextView) view.findViewById(R.id.user_cno);
        tv_email = (TextView) view.findViewById(R.id.user_email);
        tv_address = (TextView) view.findViewById(R.id.user_address);

        //upload photo
        startDialog();

        DisplayImage();
        fullname.setText(firstname + " " + lastname);
        tv_uname.setText(username);
        tv_cno.setText(cno);
        tv_email.setText(email);
        tv_address.setText(address);

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

        return view;
    }

on Activity Result()

private void startDialog(){
        final String[] items = new String[] {"From Cam", "From SD Card"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item, items);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Choose Action: ");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File file = new File(Environment.getExternalStorageDirectory(), "tmp_avatar" + String.valueOf(System.currentTimeMillis())
                            + ".jpg");
                    imageCaptureUri = Uri.fromFile(file);
                    try {
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri);
                        intent.putExtra("return data", true);

                        startActivityForResult(intent, CAMERA_REQUEST);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    dialog.cancel();
                } else {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Complete Action using"), FILE_REQUEST);
                }
            }
        });
        final AlertDialog dialog = builder.create();
        //image
        txt_image_path = (TextView) view.findViewById(R.id.image_path);
        img_logo = (ImageView) view.findViewById(R.id.display_image);
        img_logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

尝试使用此修改后的代码来查找logcat中的错误

@Override
    public void onActivityResult(int requestCode,int resultCode,
    strong textIntent  data
    {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode != Activity.RESULT_OK)
        return;
    Bitmap bitmap = null;
    String path = "";
    if(requestCode == FILE_REQUEST){
        imageCaptureUri = data.getData();
        path = getRealPathFromURI(imageCaptureUri);
        if(path != null){
            bitmap = BitmapFactory.decodeFile(path);
        }else{
            path = imageCaptureUri.getPath();
        }

    }else if(requestCode == CAMERA_REQUEST &&
    resultCode ==   RESULT_OK) {

    try
    {path = imageCaptureUri.getPath();
        bitmap = BitmapFactory.decodeFile(path);
        img_logo.setImageBitmap(bitmap);
        txt_image_path.setText(path);
       }
        catch (NullPointerException e) {
             e.printStackTrace();

    }



    }